运算符 $in
将字段的值与可能值的数组匹配。 运算符 $in
筛选出字段值等于指定的数值的文档。
语法
{
field: {
$in: [ listOfValues ]
}
}
参数
参数 | DESCRIPTION |
---|---|
field |
要匹配的字段 |
[listOfValues] |
一个用于与指定字段进行比较的值数组 |
例子
让我们了解 stores
数据集中的示例 json 的用法。
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile",
"location": {
"lat": 60.1441,
"lon": -141.5012
},
"staff": {
"totalStaff": {
"fullTime": 2,
"partTime": 0
}
},
"sales": {
"salesByCategory": [
{
"categoryName": "DJ Headphones",
"totalSales": 35921
}
],
"fullSales": 3700
},
"promotionEvents": [
{
"eventName": "Bargain Blitz Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 3,
"Day": 11
},
"endDate": {
"Year": 2024,
"Month": 2,
"Day": 18
}
},
"discounts": [
{
"categoryName": "DJ Turntables",
"discountPercentage": 18
},
{
"categoryName": "DJ Mixers",
"discountPercentage": 15
}
]
}
],
"tag": [
"#ShopLocal",
"#SeasonalSale",
"#FreeShipping",
"#MembershipDeals"
],
"company": "Lakeshore Retail",
"city": "Port Cecile",
"lastUpdated": {
"$date": "2024-12-11T10:21:58.274Z"
}
}
示例 1 - 使用 $in 运算符作为比较查询来查找具有特定类别促销的商店
查询查找通过促销活动在“熏鲑鱼”或“Anklet”类别中提供折扣的商店。
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
}
)
查询返回数组discounts
包含任一discountPercentage
15
20
元素或元素且仅显示complete discounts array
这些文档的文档。
{
"_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 }
]
}
]
}