Sometimes, we want to search an array of objects in MongoDB.
In this article, we’ll look at how to search an array of objects in MongoDB.
How to search an array of objects in MongoDB?
To search an array of objects in MongoDB, we can use the $elemMatch
operator.
For instance, we write
db.users.find({
awards: {
$elemMatch: {
award: 'Medal',
year: 1975
}
}
})
to search the awards
array in the users
collection for users
with an awards
entry that has the award
property set to 'Medal'
and year
set to 1975.
Conclusion
To search an array of objects in MongoDB, we can use the $elemMatch
operator.