To search in array of object in JavaScript MongoDB, we use the $elemMatch
property.
For instance, we write
db.users.find({
awards: { $elemMatch: { award: "National Medal", year: 1975 } },
});
to call find
with an object that has awars.$elemMatch
set to an object with the field values we’re looking for to get the values in the awards
array field that has the property values listed.