Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Query for multiple parameters in MongoDB?
To query for multiple parameters in MongoDB, use multiple field-value pairs in the query document separated by commas. For nested fields, use dot notation to access specific properties within embedded documents or arrays.
Syntax
db.collection.find({
"field1": "value1",
"field2": "value2",
"nestedField.subField": "value3"
});
Sample Data
db.multipleParametersDemo.insertMany([
{
"CustomerName": "Larry",
"CustomerDetails": [
{
"CustomerCountryName": "US",
"CustomerBankName": "HDFC",
"CustomerBalance": 17363
}
],
"Purchase": 1456
},
{
"CustomerName": "John",
"CustomerDetails": [
{
"CustomerCountryName": "UK",
"CustomerBankName": "ICICI",
"CustomerBalance": 25000
}
],
"Purchase": 2500
}
]);
{
"acknowledged": true,
"insertedIds": [
ObjectId("5cd10f9ce3526dbddbbfb60a"),
ObjectId("5cd10f9ce3526dbddbbfb60b")
]
}
Example: Query with Multiple Parameters
Find customers with name "Larry" and country "US" ?
db.multipleParametersDemo.find({
"CustomerName": "Larry",
"CustomerDetails.CustomerCountryName": "US"
});
{
"_id": ObjectId("5cd10f9ce3526dbddbbfb60a"),
"CustomerName": "Larry",
"CustomerDetails": [
{
"CustomerCountryName": "US",
"CustomerBankName": "HDFC",
"CustomerBalance": 17363
}
],
"Purchase": 1456
}
Count Matching Documents
db.multipleParametersDemo.find({
"CustomerName": "Larry",
"CustomerDetails.CustomerCountryName": "US"
}).count();
1
Key Points
- Multiple conditions in the same query document create an AND operation by default
- Use dot notation for nested fields:
"arrayField.nestedField" - All conditions must match for a document to be returned
Conclusion
MongoDB supports querying multiple parameters by specifying multiple field-value pairs in the query document. Use dot notation to access nested fields within embedded documents or arrays for precise filtering.
Advertisements
