$in

运算符 $in 将字段的值与可能值的数组匹配。 运算符 $in 筛选出字段值等于指定的数值的文档。

Syntax

{
    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 运算符作为比较查询来查找具有特定类别促销的商店

此查询查找通过促销活动在“熏鲑鱼”或“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
})

此查询返回以下结果:

[
    {
      "_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 }
          ]
        }
      ]
    }
]