How do you perform a multi-condition query in MongoDB?
In MongoDB, you can use the $and and $or operators to perform multi-condition queries.
- What is your budget?
db.collection.find({
$and: [
{ field1: value1 },
{ field2: value2 }
]
})
- “either”
db.collection.find({
$or: [
{ field1: value1 },
{ field2: value2 }
]
})
In the example above, field1 and field2 are the fields to be matched, while value1 and value2 are the values to be matched. The conditions can be combined together based on actual needs to achieve complex multi-condition queries.