- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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 for multiple parameters in MongoDB?
To query for multiple parameters in MongoDB, you can use the dot(.) notation. Let us first create a collection with documents −
> db.multipleParametersDemo.insertOne( ... { ... "CustomerName" : "Larry", ... "CustomerDetails" : [ ... { ... "CustomerCountryName" : "US", ... "CustomerBankName" : "HDFC", ... "CustomerBalance" : 17363, ... } ... ], ... "Purchase" : 1456, ... ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd10f9ce3526dbddbbfb60a") }
Following is the query to display all documents from a collection with the help of find() method −
> db.multipleParametersDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd10f9ce3526dbddbbfb60a"), "CustomerName" : "Larry", "CustomerDetails" : [ { "CustomerCountryName" : "US", "CustomerBankName" : "HDFC", "CustomerBalance" : 17363 } ], "Purchase" : 1456 }
Following is how you can query for multiple parameters in MongoDB −
> db.multipleParametersDemo.find({CustomerName: 'Larry', 'CustomerDetails.CustomerCountryName': 'US'}).count();
This will produce the following output −
1
Advertisements