Categories
JavaScript Answers

How to search in array of object in JavaScript MongoDB?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *