$or - Amazon DocumentDB

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

$or

$or 연산자는 두 개 이상의 표현식 배열에서 논리적 OR 작업을 수행하는 데 사용됩니다. 표현식 중 하나 이상과 일치하는 문서를 반환합니다. 이 연산자는 여러 조건 중 하나를 충족하는 문서를 쿼리해야 할 때 유용합니다.

파라미터

  • expression1: 평가할 첫 번째 표현식입니다.

  • expression2: 평가할 두 번째 표현식입니다.

  • ...: 평가할 추가 표현식(선택 사항).

예제(MongoDB 쉘)

다음 예제에서는 연$or산자를 사용하여 make가 모델 "Heavy H1"의 "TruckForYou" 또는 모델 "Bolid 1"의 "SportForYou"인 문서를 찾는 방법을 보여줍니다.

샘플 문서 생성

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