
- 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
Query nested array by more than one condition in MongoDB
To query nested array, use $elemMatch in MongoDB. Let us create a collection with documents −
> db.demo203.insertOne({ ... "_id" : "101", ... "Name" : "Chris", ... "details1" : [ ... { ... "empName" : "David", ... "salary" : "50000", ... "technology" : [ ... "MySQL", ... "MongoDB" ... ] ... }, ... { ... "empName" : "Carol", ... "salary" : "70000", ... ... "technology" : [ ... "Java", ... "Spring" ... ] ... } ... ] ...} ...); { "acknowledged" : true, "insertedId" : "101" }
Display all documents from a collection with the help of find() method −
> db.demo203.find();
This will produce the following output −
{ "_id" : "101", "Name" : "Chris", "details1" : [ { "empName" : "David", "salary" : "50000", "technology" : [ "MySQL", "MongoDB" ] }, { "empName" : "Carol", "salary" : "70000", "technology" : [ "Java", "Spring" ] } ] }
Here is how to query nested array by more than one condition −
> db.demo203.find( ... {details1: { $elemMatch:{"technology" : "MySQL", "empName":"David"}}} ... ).pretty()
This will produce the following output −
{ "_id" : "101", "Name" : "Chris", "details1" : [ { "empName" : "David", "salary" : "50000", "technology" : [ "MySQL", "MongoDB" ] }, { "empName" : "Carol", "salary" : "70000", "technology" : [ "Java", "Spring" ] } ] }
- Related Articles
- MongoDB query to filter object where all elements from nested array match the condition
- MongoDB query to aggregate nested array
- MongoDB query to sort nested array?
- MongoDB query where all array items are less than a specified condition?
- MongoDB query where all array items are greater than a specified condition?
- Query array of nested string with MongoDB?
- Set condition in MongoDB nested document?
- MongoDB query to get array of nested string?
- Query a nested field within an array with MongoDB
- MongoDB query to get only specific fields in nested array documents?
- Query deeply nested Objects in MongoDB
- MongoDB query to insert an array element with a condition?
- Group query upon nested object in MongoDB?
- MongoDB $push in nested array?
- How to query documents by a condition on the subdocument in MongoDB?

Advertisements