$or
The $or operator is used to perform a logical OR operation on an array of two or more expressions. It returns documents that match at least one of the expressions. This operator is useful when you need to query for documents that satisfy any one of multiple conditions.
Parameters
-
expression1: The first expression to evaluate. -
expression2: The second expression to evaluate. -
...: Additional expressions to evaluate (optional).
Example (MongoDB Shell)
The following example demonstrates the usage of the $or operator to find documents where the make is either "TruckForYou" with the model "Heavy H1" or "SportForYou" with the model "Bolid 1".
Create sample documents
db.cars.insertMany([ { make: "TruckForYou", model: "Heavy H1", year: 2020 }, { make: "SportForYou", model: "Bolid 1", year: 2021 }, { make: "TruckForYou", model: "Cargo 5", year: 2019 }, { make: "SportForYou", model: "Racer 2", year: 2022 } ]);
Query example
db.cars.find({ $or: [ { make: "TruckForYou", model: "Heavy H1" }, { make: "SportForYou", model: "Bolid 1" } ] });
Output
[
{
_id: ObjectId('...'),
make: 'TruckForYou',
model: 'Heavy H1',
year: 2020
},
{
_id: ObjectId('...'),
make: 'SportForYou',
model: 'Bolid 1',
year: 2021
}
]
Code examples
To view a code example for using the $or command, choose the tab for the language that you want to use: