
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
MongoDB query to match documents that contain an array field
To match documents that contain an array field, use the $elemMatch operator. Let us create a collection with documents −
> db.demo592.insertOne( ... { ... "id":101, ... "details" : [ ... { "Name" : "Chris", "Value" : "200"}, ... {"Name" : "David", "Value" : "800"} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e930d8ffd2d90c177b5bcd6") } > db.demo592.insertOne( ... { ... id:102, ... "details" : [ ... { "Name" : "Chris", "Value" : "500"}, ... {"Name" : "David", "Value" : "900"} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e930d90fd2d90c177b5bcd7") }
Display all documents from a collection with the help of find() method −
> db.demo592.find();
This will produce the following output −
{ "_id" : ObjectId("5e930d8ffd2d90c177b5bcd6"), "id" : 101, "details" : [ { "Name" : "Chris", "Value" : "200" }, { "Name" : "David", "Value" : "800" } ] } { "_id" : ObjectId("5e930d90fd2d90c177b5bcd7"), "id" : 102, "details" : [ { "Name" : "Chris", "Value" : "500" }, { "Name" : "David", "Value" : "900" } ] }
Following is the query to match −
> db.demo592.find( ... { ... "$and" : [ ... { "details" : { "$elemMatch" : { "Name" : "Chris", "Value" : { "$gte" : "500" } } } }, ... { "details" : { "$elemMatch" : { "Name" : "David", "Value" : { "$gte" : "600" } } } } ... ] ... } ... );
This will produce the following output −
{ "_id" : ObjectId("5e930d90fd2d90c177b5bcd7"), "id" : 102, "details" : [ { "Name" : "Chris", "Value" : "500" }, { "Name" : "David", "Value" : "900" } ] }
- Related Articles
- MongoDB query to collectively match intersection of documents against a field
- MongoDB query to match each element in a documents array to a condition?
- MongoDB query to match documents whose _id is in an array as part of a subdocument?
- MongoDB query to match documents with array values greater than a specific value
- MongoDB query to match and remove element from an array?
- MongoDB query to find matching documents given an array with values?
- Find MongoDB documents that contains specific field?
- MongoDB query (aggregation framework) to match a specific field value
- MongoDB query to get specific list of names from documents where the value of a field is an array
- Query a nested field within an array with MongoDB
- Query an array of embedded documents in MongoDB and push another?
- Find documents that contains specific field in MongoDB?
- MongoDB query to skip documents
- MongoDB query to convert an array to a map of documents with n attributes?
- MongoDB query to update array with another field?

Advertisements