$in

The `$in` operator matches values of a field against an array of possible values.

Syntax

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

Parameters

fieldstringrequired

The field to compare

listOfValuesobjectrequired

An array of values to match against

Examples

Find stores with specific categories of promotions

This query finds stores that offer discounts in either "Smoked Salmon" or "Anklets" categories via promotion events.

Query:

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

Output:

[{ "_id": "48fcdab8-b961-480e-87a9-19ad880e9a0a", "name": "Lakeshore Retail | Jewelry Collection - South Nicholas" }]

Search for specified values in nested arrays

This query searches for documents where at least one discountPercentage within promotionEvents.discounts is either 15 or 20.

Query:

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

Output:

[{ "_id": "48fcdab8-b961-480e-87a9-19ad880e9a0a", "name": "Lakeshore Retail | Jewelry Collection - South Nicholas" }]

Related