
- 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 match multiple criteria inside an array with MongoDB?
To match multiple criteria inside an array, use aggregate(). Let us create a collection with documents −
> db.demo84.insertOne({ ... "EmployeeDetails": [ ... {Name: 'John', Salary:45000, isMarried: true}, ... {Name: 'Chris', Salary:50000, isMarried: false} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2c0a3471bf0181ecc422a5") } > db.demo84.insertOne({ ... "EmployeeDetails": [ ... {Name: 'Sam', Salary:56000, isMarried: false}, ... {Name: 'Bob', Salary:50000, isMarried: false} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2c0a4071bf0181ecc422a6") }
Display all documents from a collection with the help of find() method −
> db.demo84.find();
This will produce the following output −
{ "_id" : ObjectId("5e2c0a3471bf0181ecc422a5"), "EmployeeDetails" : [ { "Name" : "John", "Salary" : 45000, "isMarried" : true }, { "Name" : "Chris", "Salary" : 50000, "isMarried" : false } ] } { "_id" : ObjectId("5e2c0a4071bf0181ecc422a6"), "EmployeeDetails" : [ { "Name" : "Sam", "Salary" : 56000, "isMarried" : false }, { "Name" : "Bob", "Salary" : 50000, "isMarried" : false } ] }
Following is the query to match multiple criteria inside an array −
Example
> db.demo84.aggregate( ... { "$match": { ... "EmployeeDetails": { ... "$elemMatch": { ... "Name": "Chris", ... "isMarried": false ... } ... } ... }} ... );
This will produce the following output −
{ "_id" : ObjectId("5e2c0a3471bf0181ecc422a5"), "EmployeeDetails" : [ { "Name" : "John", "Salary" : 45000, "isMarried" : true }, { "Name" : "Chris", "Salary" : 50000, "isMarried" : false } ] }
- Related Articles
- Match multiple criteria inside an array with MongoDB?
- Find value in a MongoDB Array with multiple criteria?
- MongoDB query to find value in array with multiple criteria (range)
- MongoDB query to find documents having two values in an array conforming to multiple criteria?
- MongoDB query to find multiple matchings inside array of objects?
- How to match date with MongoDB $match?
- MongoDB aggregation with equality inside array?
- How to update array with multiple conditions in MongoDB
- How to get the matching document inside an array in MongoDB?
- Query MongoDB with length criteria?
- Update elements inside an array in MongoDB?
- How to push new items to an array inside of an object in MongoDB?
- How to insert an item to an array that is inside an object in MongoDB?
- How do I work with array fields in MongoDB to match all?
- MongoDB aggregate to convert multiple documents into single document with an array?

Advertisements