mongodbHow to find in array in MongoDB?
MongoDB provides a powerful $elemMatch operator to query array elements. This operator can be used to match a single element or multiple elements of an array.
Example
db.collection.find(
{ arrayField: { $elemMatch: { <query1>, <query2>, ... } } }
)
This example will return documents where the arrayField contains at least one element that matches all the query criteria.
Code explanation
db.collection.find- This is the MongoDB command to query a collection.{ arrayField: { $elemMatch: { <query1>, <query2>, ... } } }- This is the query criteria to match array elements.arrayFieldis the name of the array field,$elemMatchis the operator to match array elements, and<query1>, <query2>, ...are the query criteria for the array elements.
Helpful links
More of Mongodb
- How to use watch in MongoDB?
- How to use triggers in MongoDB?
- How to list MongoDB users?
- How to remove a field from MongoDB?
- How to use MongoDB queue?
- How to use insertone in MongoDB?
- How to work with time series data in MongoDB?
- How to update an array element in MongoDB?
- How to do text search in MongoDB?
- How to update one document in MongoDB?
See more codes...