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 use watch in MongoDB?
- How to update one document in MongoDB?
- How to update many documents in MongoDB?
- How to use MongoDB queue?
- How to create a many to many relation in MongoDB?
- How to insert new document into MongoDB?
- How to use hint in MongoDB?
- How to work with time series data in MongoDB?
- How to use unwind in MongoDB?
See more codes...