$or - Amazon DocumentDB

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

$or

$or 演算子は、2 つ以上の式の配列に対して論理 OR オペレーションを実行するために使用されます。少なくとも 1 つの式に一致するドキュメントを返します。この演算子は、複数の条件のいずれかを満たすドキュメントをクエリする必要がある場合に便利です。

パラメータ

  • expression1: 評価する最初の式。

  • expression2: 評価する 2 番目の式。

  • ...: 評価する追加の式 (オプション)。

例 (MongoDB シェル)

次の例は、 がモデル「Heavy H1」のTruckForYou」またはモデル「Bolid 1」のSportForYoumake」のいずれかであるドキュメントを検索する$or演算子の使用を示しています。

サンプルドキュメントを作成する

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 } ]);

クエリの例

db.cars.find({ $or: [ { make: "TruckForYou", model: "Heavy H1" }, { make: "SportForYou", model: "Bolid 1" } ] });

出力

[ { _id: ObjectId('...'), make: 'TruckForYou', model: 'Heavy H1', year: 2020 }, { _id: ObjectId('...'), make: 'SportForYou', model: 'Bolid 1', year: 2021 } ]

コードの例

$or コマンドを使用するコード例を表示するには、使用する言語のタブを選択します。

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()