Monday, 13 March 2017

Finding a subdocument in MongoDB using Mongoose

As the title suggests I want to find a subdocument using a custom identification that I gave it. I did a similar thing with finding documents. I gave each one a custom identification as such:

schema = {
   ...
   identification: {type: String, "default":"foobar"},
   something: [array of somethings],
   ...
}

So I searched the model object like following:

Model
   .findOne({ identification: "foobar" })
   .exec(callback);

The problem is not that I do not know how to query the model to find me a subdocument, for example to find a specific something in something from the example above. I imagine there's a similar function to .findOne() for subdocuments, but I just can't seem to find it on Google. However, if you have a better suggestion for identification of documents and subdocuments, anything that I can set myself (meaning that it's not random letters and numbers), I'm open to suggestions. Thank you in advance! Cheers!



via ficabj5

No comments:

Post a Comment