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 update one document in MongoDB?
- How to create a many to many relation in MongoDB?
- How to use triggers in MongoDB?
- How to drop a collection in MongoDB?
- How to empty an array in MongoDB?
- How to update many documents in MongoDB?
- How to use unwind in MongoDB?
- How to bind IP addresses for MongoDB server?
- How to use watch in MongoDB?
- How to use MongoDB queue?
See more codes...