Answer the question
In order to leave comments, you need to log in
How to find an entry in a nested array of MongoDB objects?
Good evening, there is a record. It is necessary to find a user with ID 1, and extract an object with _id 24123123dsfasdf32 from his items field
{
id: 1,
items: [
{
_id: 24123123dsfasdf32,
text: 'string'
}
]
},
{
id: 2,
items: [
{
_id: 2sdfsdfsdf4123123dsfasdf32,
text: 'string2'
}
]
}
Answer the question
In order to leave comments, you need to log in
To solve the problem, it is enough to use $elemMatch. For the current model it will be like this:
Model.findOne({id: 1}, {items: {$elemMatch: {_id: 24123123dsfasdf32}}).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question