
- 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
How to filter documents based on an array in MongoDB?
To filter documents based on an array, use $elemMatch. The $elemMatch operator matches documents that contain an array field.
Let us create a collection with documents −
> db.demo453.insertOne( ... { _id: 101, details: [ { Name: "David", Marks: 60 }, { Name: "Mike", Marks: 55} ] } ... ) { "acknowledged" : true, "insertedId" : 101 } > db.demo453.insertOne( ... { _id: 102, details: [ { Name: "Bob", Marks: 80 }, { Name: "Sam", Marks: 78} ] } ... ) { "acknowledged" : true, "insertedId" : 102 } > db.demo453.insertOne( ... { _id: 103, details: [ { Name: "Carol", Marks: 67 }, { Name: "John", Marks: 79} ] } ... ) { "acknowledged" : true, "insertedId" : 103 }
Display all documents from a collection with the help of find() method −
> db.demo453.find();
This will produce the following output −
{ "_id" : 101, "details" : [ { "Name" : "David", "Marks" : 60 }, { "Name" : "Mike", "Marks" : 55 } ] } { "_id" : 102, "details" : [ { "Name" : "Bob", "Marks" : 80 }, { "Name" : "Sam", "Marks" : 78 } ] } { "_id" : 103, "details" : [ { "Name" : "Carol", "Marks" : 67 }, { "Name" : "John", "Marks" : 79 } ] }
Following is the query to filter documents based on an array −
> db.demo453.find( ... { details: { $elemMatch: {Marks: { $gte: 75 } } } } ... )
This will produce the following output −
{ "_id" : 102, "details" : [ { "Name" : "Bob", "Marks" : 80 }, { "Name" : "Sam", "Marks" : 78 } ] } { "_id" : 103, "details" : [ { "Name" : "Carol", "Marks" : 67 }, { "Name" : "John", "Marks" : 79 } ] }
- Related Articles
- Querying array of Embedded Documents in MongoDB based on Range?
- Filter an object based on an array JavaScript
- Aggregate based on array value to sum values in different MongoDB documents?
- How can I find documents in MongoDB based on the number of matched objects within an array?
- How to filter object array based on attributes in jQuery?
- Filter array based on another array in JavaScript
- MongoDB filter multiple sub-documents?
- Filter an array containing objects based on another array containing objects in JavaScript
- How to search documents through an integer array in MongoDB?
- How to find MongoDB documents in a collection with a filter on multiple combined fields?
- How would I filter out sub documents in MongoDB?
- Filter documents in MongoDB using simple query?
- MongoDB aggregation to sum individual properties on an object in an array across documents
- How to aggregate array documents in MongoDB?
- How to filter array elements in MongoDB?

Advertisements