연산자는 $in 필드의 값을 가능한 값 배열과 일치합니다.
$in 연산자는 필드 값이 지정된 값과 동일한 문서를 필터링합니다.
문법
{
field: {
$in: [ listOfValues ]
}
}
예시
스토어 컬렉션에서 이 샘플 문서를 고려합니다.
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
"location": {
"lat": -89.2384,
"lon": -46.4012
},
"staff": {
"totalStaff": {
"fullTime": 8,
"partTime": 20
}
},
"sales": {
"totalSales": 75670,
"salesByCategory": [
{
"categoryName": "Wine Accessories",
"totalSales": 34440
},
{
"categoryName": "Bitters",
"totalSales": 39496
},
{
"categoryName": "Rum",
"totalSales": 1734
}
]
},
"promotionEvents": [
{
"eventName": "Unbeatable Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 6,
"Day": 23
},
"endDate": {
"Year": 2024,
"Month": 7,
"Day": 2
}
},
"discounts": [
{
"categoryName": "Whiskey",
"discountPercentage": 7
},
{
"categoryName": "Bitters",
"discountPercentage": 15
},
{
"categoryName": "Brandy",
"discountPercentage": 8
},
{
"categoryName": "Sports Drinks",
"discountPercentage": 22
},
{
"categoryName": "Vodka",
"discountPercentage": 19
}
]
},
{
"eventName": "Steal of a Deal Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 29
}
},
"discounts": [
{
"categoryName": "Organic Wine",
"discountPercentage": 19
},
{
"categoryName": "White Wine",
"discountPercentage": 20
},
{
"categoryName": "Sparkling Wine",
"discountPercentage": 19
},
{
"categoryName": "Whiskey",
"discountPercentage": 17
},
{
"categoryName": "Vodka",
"discountPercentage": 23
}
]
}
]
}
예제 1 - $in 연산자를 비교 쿼리로 사용하여 특정 범주의 승격이 있는 저장소를 찾습니다.
이 쿼리는 프로모션 이벤트를 통해 "훈제 연어" 또는 "앵클" 범주에서 할인을 제공하는 매장을 찾습니다.
db.stores.find({
"promotionEvents.discounts.categoryName": {
$in: ["Smoked Salmon", "Anklets"]
}
}, {
name: 1,
"promotionEvents.discounts.categoryName": 1
}).limit(1)
이 쿼리에서 반환되는 첫 번째 결과는 다음과 같습니다.
[
{
"_id": "48fcdab8-b961-480e-87a9-19ad880e9a0a",
"name": "Lakeshore Retail | Jewelry Collection - South Nicholas",
"promotionEvents": [
{
"discounts": [
{"categoryName": "Anklets"},
{"categoryName": "Cufflinks"}
]
},
{
"discounts": [
{"categoryName": "Anklets"},
{"categoryName": "Brooches"}
]
},
{
"discounts": [
{"categoryName": "Rings"},
{"categoryName": "Bracelets"}
]
},
{
"discounts": [
{"categoryName": "Charms"},
{"categoryName": "Bracelets"}
]
},
{
"discounts": [
{"categoryName": "Watches"},
{"categoryName": "Pendants"}
]
}
]
}
]
예제 2 - 지정된 값 또는 값 집합에 대한 배열에서 $in 연산자를 배열 식으로 사용
이 쿼리는 지정된 저장소를 검색하고 하나 이상의 discountPercentagepromotionEvents.discounts 저장소가 15 또는 20인 문서를 필터링합니다. 점 표기법 경로와 $in 연산자를 사용하여 배열 계층 구조에서 중첩된 할인 값을 일치합니다.
db.stores.find({
_id: "48fcdab8-b961-480e-87a9-19ad880e9a0a",
"promotionEvents.discounts.discountPercentage": {
$in: [15, 20]
}
}, {
_id: 1,
name: 1,
"promotionEvents.discounts": 1
})
이 쿼리는 다음 결과를 반환합니다.
[
{
"_id": "48fcdab8-b961-480e-87a9-19ad880e9a0a",
"name": "Lakeshore Retail | Jewelry Collection - South Nicholas",
"promotionEvents": [
{
"discounts": [
{ "categoryName": "Anklets", "discountPercentage": 12 },
{ "categoryName": "Cufflinks", "discountPercentage": 9 }
]
},
{
"discounts": [
{ "categoryName": "Anklets", "discountPercentage": 23 },
{ "categoryName": "Brooches", "discountPercentage": 12 }
]
},
{
"discounts": [
{ "categoryName": "Rings", "discountPercentage": 10 },
{ "categoryName": "Bracelets", "discountPercentage": 21 }
]
},
{
"discounts": [
{ "categoryName": "Charms", "discountPercentage": 9 },
{ "categoryName": "Bracelets", "discountPercentage": 13 }
]
},
{
"discounts": [
{ "categoryName": "Watches", "discountPercentage": 20 },
{ "categoryName": "Pendants", "discountPercentage": 7 }
]
}
]
}
]
관련 콘텐츠
- MongoDB에서 Azure Cosmos DB for MongoDB(vCore)로 마이그레이션하기 위한 옵션을 검토합니다.
- MongoDB와의 기능 호환성에 대해 자세히 알아보세요.