 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP 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"          ]       }    ] }Advertisements
 