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()?

0

1 Answer 1

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 } )

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.