運算符 $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 運算子作為陣列運算式
此查詢會搜尋指定的存放區,並過濾任何內discountPercentage至少有一個promotionEvents.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 }
]
}
]
}
]