Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.find does not work #28

Closed
sibelius opened this issue Jul 16, 2020 · 6 comments
Closed

.find does not work #28

sibelius opened this issue Jul 16, 2020 · 6 comments
Labels
question Further information is requested

Comments

@sibelius
Copy link

my schema has a field of type Buffer subtype 4 (uuid), then I'm trying to use this package to find data based on uuid

import MUUID from 'uuid-mongodb';

const Data = new mongoose.Schema({
uuid: {
      type: Buffer,
      subtype: 4,
      default: () => uuidv4(),

      required: true,
      unique: true,
      index: true,
    },
})

const data = new Data({}).save();

const all = await Data.findOne({ uuid: MUUID.from(data.uuid.toString()})

all is returning an empty array

@cdimascio
Copy link
Owner

@sibelius
Coupe notes

  • be sure wait for save to commit (use await) before fetching the record
  • you need to create a model from the schema

assuming that is in place you can do the following:

    // create a v4 uuid and wrap it in an MUUID (this is a shortcut for convenience) 
    const uuid = MUUID.v4();

    // save record and wait for it to commit
    await new Data({ uuid }).save();

    // retrieve the record
    const result = await Data.findOne({ uuid });

    // output the result
    console.log(result);

@cdimascio
Copy link
Owner

full working source

const mongoose = require('mongoose');
const MUUID = require('uuid-mongodb');

// Setup and connect
mongoose.connect('mongodb://localhost/my_mongoose', {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

const db = mongoose.connection
  .on('error', () => console.error('connection error:'))
  .once('open', () => {});

async function main() {
  const dataSchema = new mongoose.Schema({
    uuid: {
      type: 'object',
      value: { type: 'Buffer' },
      default: () => MUUID.v4(),

      required: true,
      unique: true,
      index: true,
    },
  });
  const Data = mongoose.model('Data', dataSchema);

  try {
    // create a v4 uuid (this simply wraps the fantastic uuid library)
    const uuid = MUUID.v4();

    // save record and wait for it to commit
    await new Data({ uuid }).save();

    // retrieve the record
    const result = await Data.findOne({ uuid });

    // output the result
    console.log(result);
  } catch (e) {
    console.error(e);
  } finally {
    db.close()
  }
}

main();

@cdimascio cdimascio added the question Further information is requested label Jul 18, 2020
@cdimascio
Copy link
Owner

@sibelius let me know if this helps. best!

@sibelius
Copy link
Author

Find is working even with pure string , aggregates does not, you need to transform the data yourself

Tks

@cdimascio
Copy link
Owner

cdimascio commented Jul 18, 2020

Can you send an example. I'll see if there is something that that can improve your case

@sibelius
Copy link
Author

sibelius commented Jul 20, 2020

what worked for us, was this

uuid: new mongoose.Types.Buffer(value).toObject(0x04)

value as the uuid as a string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants