$in

Operator $in porównuje wartości pola z tablicą możliwych wartości. Operator $in filtruje dokumenty, w których wartość pola jest równa dowolnej z określonych wartości.

Składnia

{
    field: {
        $in: [ listOfValues ]
    }
}

Przykłady

Rozważmy ten przykładowy dokument z kolekcji sklepów.

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

Przykład 1 — użyj operatora $in jako zapytania porównawczego, aby znaleźć sklep z określonymi kategoriami promocji

To zapytanie znajduje sklepy, które oferują rabaty w kategoriach "Wędzony łosoś" lub "Kostki" za pośrednictwem zdarzeń promocji.

db.stores.find({
    "promotionEvents.discounts.categoryName": {
        $in: ["Smoked Salmon", "Anklets"]
    }
}, {
    name: 1,
    "promotionEvents.discounts.categoryName": 1
}).limit(1)

Pierwszy wynik zwrócony przez to zapytanie to:

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

Przykład 2 — użyj operatora $in jako wyrażenia tablicowego w tablicy dla określonej wartości lub zestawu wartości

To zapytanie wyszukuje określony magazyn i filtruje dokumenty, w których co najmniej jeden discountPercentage w obrębie każdego z nich promotionEvents.discounts wynosi 15 lub 20. Używa ścieżki notacji kropkowej i operatora $in, aby dopasować zagnieżdżone wartości rabatu w hierarchii tablicy.

db.stores.find({
    _id: "48fcdab8-b961-480e-87a9-19ad880e9a0a",
    "promotionEvents.discounts.discountPercentage": {
        $in: [15, 20]
    }
}, {
    _id: 1,
    name: 1,
    "promotionEvents.discounts": 1
})

To zapytanie zwraca następujący wynik:

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