$setEquals - Amazon DocumentDB

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

$setEquals

Amazon DocumentDB의 $setEquals 연산자는 두 세트가 동일한지 확인하는 데 사용됩니다. 두 배열을 비교하고 순서에 관계없이 동일한 고유 요소를 포함하는 true 경우를 반환합니다.

파라미터

  • expression1: 비교할 첫 번째 배열입니다.

  • expression2: 비교할 두 번째 배열입니다.

예제(MongoDB 쉘)

다음 예제에서는 연$setEquals산자를 사용하여 두 값 세트를 비교하는 방법을 보여줍니다.

샘플 문서 생성

db.collection.insertMany([ { _id: 1, fruits: ["apple", "banana", "cherry"] }, { _id: 2, fruits: ["banana", "apple", "cherry"] }, { _id: 3, fruits: ["apple", "banana", "orange"] } ])

쿼리 예제

db.collection.find({ $expr: { $setEquals: ["$fruits", ["apple", "banana", "cherry"]] } })

출력

{ "_id" : 1, "fruits" : [ "apple", "banana", "cherry" ] } { "_id" : 2, "fruits" : [ "banana", "apple", "cherry" ] }

쿼리는 $setEquals 연산자를 사용하여 각 문서의 fruits 필드를 배열과 비교합니다["apple", "banana", "cherry"]. fruits 필드가 비교 배열과 동일한 문서가 반환됩니다.

코드 예제

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

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('mycollection'); // Insert sample documents await collection.insertMany([ { _id: 1, fruits: ["apple", "banana", "cherry"] }, { _id: 2, fruits: ["banana", "apple", "cherry"] }, { _id: 3, fruits: ["apple", "banana", "orange"] } ]); // Query using $setEquals const result = await collection.find({ $expr: { $setEquals: ["$fruits", ["apple", "banana", "cherry"]] } }).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['mycollection'] # Insert sample documents collection.insert_many([ {"_id": 1, "fruits": ["apple", "banana", "cherry"]}, {"_id": 2, "fruits": ["banana", "apple", "cherry"]}, {"_id": 3, "fruits": ["apple", "banana", "orange"]} ]) # Query using $setEquals result = list(collection.find({ "$expr": { "$setEquals": ["$fruits", ["apple", "banana", "cherry"]] } })) print(result) client.close() example()