문서 메뉴
문서 홈
/
MongoDB 매뉴얼
/ / /

$setIntersection (애그리게이션)

이 페이지의 내용

  • 정의
  • 행동
  • 예제
$setIntersection

두 개 이상의 배열을 가져와 모든 입력 배열에 나타나는 요소가 포함된 배열을 반환합니다.

$setIntersection 의 구문은 다음과 같습니다:

{ $setIntersection: [ <array1>, <array2>, ... ] }

인수는 각각 배열로 해석되는 한 유효한 표현식 이 될 수 있습니다. 표현식에 대한 자세한 내용은 표현식 연산자를 참조하세요.

$setIntersection은 배열에 집합 연산을 수행하여 배열을 집합으로 취급합니다. 배열에 중복 항목이 포함되어 있으면 $setIntersection는 중복 항목을 무시합니다. $setIntersection는 요소의 순서를 무시합니다.

$setIntersection 결과에서 중복 항목을 필터링하여 고유한 항목만 포함하는 배열을 출력합니다. 출력 배열의 요소 순서는 지정되지 않습니다.

교차점이 발견되지 않으면(즉, 입력 배열에 공통 요소가 포함되지 않은 경우) $setIntersection 은 빈 배열을 반환합니다.

세트에 중첩된 배열 요소가 포함된 경우 $setIntersection는 중첩된 배열로 내려가지 않고 최상위 수준에서 배열을 평가합니다.

예제
결과
{ $setIntersection: [ [ "a", "b", "a" ], [ "b", "a" ] ] }
[ "b", "a" ]
{ $setIntersection: [ [ "a", "b" ], [ [ "a", "b" ] ] ] }
[ ]

이 섹션에는 collection과 함께 $setIntersection를 사용하는 방법을 보여주는 예가 포함되어 있습니다.

다음 문서가 포함된 flowers 컬렉션을 생각해 보세요.

db.flowers.insertMany( [
{ "_id" : 1, "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ "rose", "orchid" ] },
{ "_id" : 2, "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ "orchid", "rose", "orchid" ] },
{ "_id" : 3, "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ "rose", "orchid", "jasmine" ] },
{ "_id" : 4, "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ "jasmine", "rose" ] },
{ "_id" : 5, "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ ] },
{ "_id" : 6, "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ [ "rose" ], [ "orchid" ] ] },
{ "_id" : 7, "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ [ "rose", "orchid" ] ] },
{ "_id" : 8, "flowerFieldA" : [ ], "flowerFieldB" : [ ] },
{ "_id" : 9, "flowerFieldA" : [ ], "flowerFieldB" : [ "rose" ] }
] )

다음 연산은 $setIntersection 연산자를 사용하여 flowerFieldA 배열과 flowerFieldB 배열 모두에 공통적인 요소의 배열을 반환합니다.

db.flowers.aggregate(
[
{ $project: { flowerFieldA: 1, flowerFieldB: 1, commonToBoth: { $setIntersection: [ "$flowerFieldA", "$flowerFieldB" ] }, _id: 0 } }
]
)

이 연산은 다음과 같은 결과를 반환합니다.

{ "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ "rose", "orchid" ], "commonToBoth" : [ "orchid", "rose" ] }
{ "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ "orchid", "rose", "orchid" ], "commonToBoth" : [ "orchid", "rose" ] }
{ "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ "rose", "orchid", "jasmine" ], "commonToBoth" : [ "orchid", "rose" ] }
{ "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ "jasmine", "rose" ], "commonToBoth" : [ "rose" ] }
{ "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ ], "commonToBoth" : [ ] }
{ "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ [ "rose" ], [ "orchid" ] ], "commonToBoth" : [ ] }
{ "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ [ "rose", "orchid" ] ], "commonToBoth" : [ ] }
{ "flowerFieldA" : [ ], "flowerFieldB" : [ ], "commonToBoth" : [ ] }
{ "flowerFieldA" : [ ], "flowerFieldB" : [ "rose" ], "commonToBoth" : [ ] }

MongoDB 7.0부터는 새로운 USER_ROLES 시스템 변수를 사용하여 사용자 역할을 반환할 수 있습니다.

이 섹션의 시나리오에서는 예산 정보가 포함된 컬렉션의 문서에 대한 액세스 권한이 제한된 다양한 역할의 사용자를 보여줍니다.

이 시나리오는 USER_ROLES의 한 가지 가능한 용도를 보여 줍니다. budget collection에는 allowedRoles(이)라는 필드가 있는 문서가 포함되어 있습니다. 다음 시나리오에서 볼 수 있듯이 allowedRoles 필드에 있는 사용자 역할을 USER_ROLES 시스템 변수에서 반환한 역할과 비교하는 쿼리를 쓸 수 있습니다.

참고

다른 USER_ROLES 예제 시나리오에 대해 알아보려면 현재 사용자에게 부여된 역할에 대한 의료 정보 조회를 참조하세요. 이 예에서는 다음 예에서처럼 문서 필드에 사용자 역할을 저장하지 않습니다.

이 섹션의 예산 시나리오의 경우 다음 단계를 수행하여 역할, 사용자 및 budget 컬렉션을 만들 수 있습니다.

1

실행:

db.createRole( { role: "Marketing", roles: [], privileges: [] } )
db.createRole( { role: "Sales", roles: [], privileges: [] } )
db.createRole( { role: "Development", roles: [], privileges: [] } )
db.createRole( { role: "Operations", roles: [], privileges: [] } )
2

필수 역할로 JohnJane이라는 이름의 사용자를 만듭니다. test 데이터베이스를 데이터베이스 이름으로 바꿉니다.

db.createUser( {
user: "John",
pwd: "jn008",
roles: [
{ role: "Marketing", db: "test" },
{ role: "Development", db: "test" },
{ role: "Operations", db: "test" },
{ role: "read", db: "test" }
]
} )
db.createUser( {
user: "Jane",
pwd: "je009",
roles: [
{ role: "Sales", db: "test" },
{ role: "Operations", db: "test" },
{ role: "read", db: "test" }
]
} )
3

실행:

db.budget.insertMany( [
{
_id: 0,
allowedRoles: [ "Marketing" ],
comment: "For marketing team",
yearlyBudget: 15000
},
{
_id: 1,
allowedRoles: [ "Sales" ],
comment: "For sales team",
yearlyBudget: 17000,
salesEventsBudget: 1000
},
{
_id: 2,
allowedRoles: [ "Operations" ],
comment: "For operations team",
yearlyBudget: 19000,
cloudBudget: 12000
},
{
_id: 3,
allowedRoles: [ "Development" ],
comment: "For development team",
yearlyBudget: 27000
}
] )

John에 액세스할 수 있는 문서를 검색하려면 다음 단계를 수행합니다.

1

실행:

db.auth( "John", "jn008" )
2

시스템 변수를 사용하려면 변수 이름 시작 부분에 $$를 추가합니다. USER_ROLES 시스템 변수를 $$USER_ROLES로 지정합니다.

실행:

db.budget.aggregate( [ {
$match: {
$expr: {
$not: {
$eq: [ { $setIntersection: [ "$allowedRoles", "$$USER_ROLES.role" ] }, [] ]
}
}
}
} ] )

이전 예제에서는 예제를 실행하는 사용자가 가진 역할 중 하나 이상과 일치하는 budget 컬렉션의 문서를 반환합니다. 이를 위해 이 예제에서는 $setIntersection 를 사용하여 budget 문서 allowedRoles 필드와 $$USER_ROLES 의 사용자 역할 집합 사이의 교집합이 비어 있지 않은 문서를 반환합니다.

3

John Marketing, OperationsDevelopment역할이 있으며 다음과 같은 문서를 볼 수 있습니다.

[
{
_id: 0,
allowedRoles: [ 'Marketing' ],
comment: 'For marketing team',
yearlyBudget: 15000
},
{
_id: 2,
allowedRoles: [ 'Operations' ],
comment: 'For operations team',
yearlyBudget: 19000,
cloudBudget: 12000
},
{
_id: 3,
allowedRoles: [ 'Development' ],
comment: 'For development team',
yearlyBudget: 27000
}
]

Jane에 액세스할 수 있는 문서를 검색하려면 다음 단계를 수행합니다.

1

실행:

db.auth( "Jane", "je009" )
2

시스템 변수를 사용하려면 변수 이름 시작 부분에 $$를 추가합니다. USER_ROLES 시스템 변수를 $$USER_ROLES로 지정합니다.

실행:

db.budget.aggregate( [ {
$match: {
$expr: {
$not: {
$eq: [ { $setIntersection: [ "$allowedRoles", "$$USER_ROLES.role" ] }, [] ]
}
}
}
} ] )
3

Jane 은(는) SalesOperations 역할을 가지며 다음과 같은 문서를 볼 수 있습니다.

[
{
_id: 1,
allowedRoles: [ 'Sales' ],
comment: 'For sales team',
yearlyBudget: 17000,
salesEventsBudget: 1000
},
{
_id: 2,
allowedRoles: [ 'Operations' ],
comment: 'For operations team',
yearlyBudget: 19000,
cloudBudget: 12000
}
]

참고

샤드 클러스터에서는 사용자를 대신하여 다른 서버 노드가 샤드에서 쿼리를 실행할 수 있습니다. 이러한 쿼리에서 USER_ROLES는 여전히 사용자에 대한 역할로 채워져 있습니다.

← $setField (집계)

이 페이지의 내용