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.condition1andcondition2: These are the conditions that will be evaluated.
Helpful links
More of Mongodb
- How to update one document in MongoDB?
- How to use unwind in MongoDB?
- How to use transactions in MongoDB?
- How to use MongoDB queue?
- How to use watch in MongoDB?
- What is MongoDB default port?
- How to join two collections in MongoDB?
- How to convert MongoDB ObjectId to string?
- How to update an array element in MongoDB?
- How to use regex in MongoDB?
See more codes...