

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- MongoDB query where all array items are less than a specified condition?
- MongoDB query where all array items are greater than a specified condition?
- MongoDB query to sort nested array?
- MongoDB query to aggregate nested array
- Use JOIN to select record with more than one condition using AND?
- Set condition in MongoDB nested document?
- MongoDB query to filter object where all elements from nested array match the condition
- Query array of nested string with MongoDB?
- MongoDB query to get array of nested string?
- Query a nested field within an array with MongoDB
- Query deeply nested Objects in MongoDB
- MongoDB query to insert an array element with a condition?
- How to query documents by a condition on the subdocument in MongoDB?
- Query MongoDB for a nested search
- MongoDB find() query for nested document?
Advertisements