$and - Amazon DocumentDB

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

$and

$and 집계 연산자는 여러 표현식을 평가하고 모든 표현식이 로 평가되는 true 경우에만를 반환합니다true. 표현식이 이면 false를 반환합니다false.

파라미터

  • expressions: 평가할 표현식의 배열입니다.

예제(MongoDB 쉘)

다음 예제에서는 $and 연산자를 사용하여 제품이 여러 기준을 충족하는지 확인하는 방법을 보여줍니다.

샘플 문서 생성

db.products.insertMany([ { _id: 1, name: "Laptop", price: 1200, inStock: true }, { _id: 2, name: "Mouse", price: 25, inStock: false }, { _id: 3, name: "Keyboard", price: 75, inStock: true } ]);

쿼리 예제

db.products.aggregate([ { $project: { name: 1, price: 1, inStock: 1, affordable: { $and: [ { $lt: ["$price", 100] }, { $eq: ["$inStock", true] } ] } } } ]);

출력

[ { _id: 1, name: 'Laptop', price: 1200, inStock: true, affordable: false }, { _id: 2, name: 'Mouse', price: 25, inStock: false, affordable: false }, { _id: 3, name: 'Keyboard', price: 75, inStock: true, affordable: true } ]

코드 예제

$and 집계 연산자 사용에 대한 코드 예제를 보려면 사용하려는 언어의 탭을 선택합니다.

Node.js
const { MongoClient } = require('mongodb'); async function example() { 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 collection = db.collection('products'); const result = await collection.aggregate([ { $project: { name: 1, price: 1, inStock: 1, affordable: { $and: [ { $lt: ["$price", 100] }, { $eq: ["$inStock", true] } ] } } } ]).toArray(); console.log(result); await client.close(); } example();
Python
from pymongo import MongoClient def example(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client['test'] collection = db['products'] result = list(collection.aggregate([ { '$project': { 'name': 1, 'price': 1, 'inStock': 1, 'affordable': { '$and': [ { '$lt': ['$price', 100] }, { '$eq': ['$inStock', True] } ] } } } ])) print(result) client.close() example()