0

So I have a collection:

{
 _id: 1,
 array: [{id:1,something: "anything"}]
},
{
 _id: 2,
 array: [{id:2,something: "anything"}]
}

I want to find the object that has a array that includes the object with the id 1, how do I do that with mongodb's .findOne()?

Onii-Chan
  • 57
  • 1
  • 6

1 Answers1

0

You can find using $elemMatch.

db.collection_name.findOne( { array : { $elemMatch : { id : 1 } } } )

This is another way you can also find using this simple query db.collection_name.findOne( { "array.id" : 1 } )

Code Tech
  • 16
  • 4