$anyElementTrue - Amazon DocumentDB

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

$anyElementTrue

버전 4.0의 새로운 기능

$anyElementTrue 연산자는 배열의 요소가 true인지 확인하는 데 사용됩니다.

파라미터

  • field: 평가할 배열 필드입니다.

예제(MongoDB 쉘)

다음 예제에서는를 사용하여 배열의 요소가 true인지 확인하는 $anyElementTrue 방법을 보여줍니다.

샘플 문서 생성

db.grades.insertMany([ { _id: 1, student: 'Tim', scores: [false, false, null] }, { _id: 2, student: 'Bob', scores: [false, 0, false] }, { _id: 3, student: 'Ivy', scores: [false, true, 0] } ])

쿼리 예제

db.grades.aggregate([ { $project: { student: 1, isAnyTrue: { $anyElementTrue: ["$scores"] }, _id: 0 } } ])

출력

[ { student: 'Tim', isAnyTrue: false }, { student: 'Bob', isAnyTrue: false }, { student: 'Ivy', isAnyTrue: true } ]

코드 예제

$anyElementTrue 명령을 사용하기 위한 코드 예제를 보려면 사용하려는 언어의 탭을 선택합니다.

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('grades'); const result = await collection.aggregate([ { $project: { student: 1, isAnyTrue: { $anyElementTrue: ["$scores"] }, _id: 0 } } ]).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['grades'] result = list(collection.aggregate([ { "$project": { "student": 1, "isAnyTrue": { "$anyElementTrue": ["$scores"] }, "_id": 0 } } ])) print(result) client.close() example()