정의
$setEquals두 개 이상의 배열을 비교하여 동일한 고유 요소가 있으면
true를 반환하고 그렇지 않으면false를 반환합니다.$setEquals의 구문은 다음과 같습니다:{ $setEquals: [ <expression1>, <expression2>, ... ] } 인수는 각각 배열로 해석되는 한 유효한 표현식 이 될 수 있습니다. 표현식에 대한 자세한 내용은 표현식을 참조하세요 .
행동
$setEquals performs set operation on arrays, treating arrays as sets. If an array contains duplicate entries, $setEquals ignores the duplicate entries. $setEquals ignores the order of the elements.
If a set contains a nested array element, $setEquals does not descend into the nested array but evaluates the array at top-level.
예시 | 결과 |
|---|---|
|
|
|
|
예시
다음 문서가 포함된 bakeryOrders collection을 생각해 보세요.
db.bakeryOrders.insertMany( [ { _id: 0, cakes: ["chocolate", "vanilla"], cupcakes: ["chocolate", "vanilla"] }, { _id: 1, cakes: ["chocolate", "vanilla"], cupcakes: ["vanilla", "chocolate"] }, { _id: 2, cakes: ["chocolate", "chocolate"], cupcakes: ["chocolate"] }, { _id: 3, cakes: ["vanilla"], cupcakes: ["chocolate"] }, { _id: 4, cakes: ["vanilla"], cupcakes: [] } ] )
The following operation uses the $setEquals operator to determine if the cakes array and the cupcakes array in each order contain the same flavors:
db.bakeryOrders.aggregate( [ { $project: { _id: 0, cakes: 1, cupcakes: 1, sameFlavors: { $setEquals: [ "$cakes", "$cupcakes" ] } } } ] )
참고
프로젝트
$project 단계에서는 출력 문서에 포함되는 필드를 지정합니다. 이 예에서 $project 단계는 다음과 같습니다.
출력에서
_id필드를 제외합니다.출력에
cakes및cupcakes필드를 포함합니다.sameFlavors라는 새 필드에$setEquals연산자의 결과를 출력합니다.
이 연산은 다음과 같은 결과를 반환합니다.
{ cakes: [ "chocolate", "vanilla" ], cupcakes: [ "chocolate", "vanilla" ], sameFlavors: true }, { cakes: [ "chocolate", "vanilla" ], cupcakes: [ "vanilla", "chocolate" ], sameFlavors: true }, { cakes: [ "chocolate", "chocolate" ], cupcakes: [ "chocolate" ], sameFlavors: true }, { cakes: [ "vanilla" ], cupcakes: [ "chocolate" ], sameFlavors: false }, { cakes: [ "vanilla" ], cupcakes: [], sameFlavors: false }