mongodbHow to use MongoDB query with "or" condition?
MongoDB query with "or" condition can be used to query multiple conditions in a single query. The syntax for this is $or
followed by an array of conditions.
Example code
db.collection.find({
$or: [
{ condition1 },
{ condition2 }
]
})
Output example
{ "_id" : ObjectId("5f3d7f9f8f9f9f9f9f9f9f9f"), "name" : "John", "age" : 25 }
{ "_id" : ObjectId("5f3d7f9f8f9f9f9f9f9f9f9f"), "name" : "Jane", "age" : 30 }
Code explanation
$or
: This is the operator used to specify multiple conditions in a single query.[ ]
: This is an array of conditions that will be evaluated.condition1
andcondition2
: These are the conditions that will be evaluated.
Helpful links
More of Mongodb
- How to check the version of MongoDB?
- How to work with time series data in MongoDB?
- How to use watch in MongoDB?
- How to empty an array in MongoDB?
- How to update an array element in MongoDB?
- How to update many documents in MongoDB?
- How to use unwind in MongoDB?
- How to use triggers in MongoDB?
- How to list MongoDB users?
- How to use the limit operator in MongoDB?
See more codes...