AI 에이전트의 경우: 문서 인덱스는 https://www.mongodb.com/ko-kr/docs/llms.txt에서 사용할 수 있으며, 모든 페이지의 마크다운 버전은 어떤 URL 경로에 .md를 추가하여 사용할 수 있습니다.
Docs Menu

$setIntersection (표현식 연산자)

$setIntersection

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

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

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

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

$setIntersection performs set operation on arrays, treating arrays as sets. If an array contains duplicate entries, $setIntersection ignores the duplicate entries. $setIntersection ignores the order of the elements.

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

If no intersections are found (i.e. the input arrays contain no common elements), $setIntersection returns an empty array.

If a set contains a nested array element, $setIntersection does not descend into the nested array but evaluates the array at top-level.

예시
결과
{ $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" ] }
] )

The following operation uses the $setIntersection operator to return an array of elements common to both the flowerFieldA array and the flowerFieldB array:

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" ] }, [] ]
}
}
}
} ] )

The previous example returns the documents from the budget collection that match at least one of the roles that the user who runs the example has. To do that, the example uses $setIntersection to return documents where the intersection between the budget document allowedRoles field and the set of user roles from $$USER_ROLES is not empty.

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는 여전히 사용자에 대한 역할로 채워져 있습니다.

이 페이지 평가하기

이 페이지의 내용