$or - Amazon DocumentDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

$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:

Node.js
const { MongoClient } = require('mongodb'); async function findCarsByMakeModel() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const cars = db.collection('cars'); const result = await cars.find({ $or: [ { make: "TruckForYou", model: "Heavy H1" }, { make: "SportForYou", model: "Bolid 1" } ] }).toArray(); console.log(result); client.close(); } findCarsByMakeModel();
Python
from pymongo import MongoClient def find_cars_by_make_model(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client['test'] cars = db.cars result = list(cars.find({ '$or': [ {'make': 'TruckForYou', 'model': 'Heavy H1'}, {'make': 'SportForYou', 'model': 'Bolid 1'} ] })) print(result) client.close() find_cars_by_make_model()