$nin

The `$nin` operator retrieves documents where the value of a specified field doesn't match any value in a list.

Syntax

{ field: { $nin: [ <listOfValues> ] } }

Parameters

fieldstringrequired

The field to compare

listOfValuesobjectrequired

An array of values that shouldn't match the value of the field being compared

Examples

Find stores with discounts that aren't 10%, 15%, or 20%

To find stores with promotions offering discounts that are not 10%, 15%, or 20%, run a query using $nin on the nested discountPercentage field.

Query:

db.stores.find({
    "promotionEvents.discounts.discountPercentage": { $nin: [10, 15, 20] }
}, { name: 1, "promotionEvents.discounts.discountPercentage": 1 }, { limit: 1 })

Output:

[{ "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f", "name": "First Up Consultants | Bed and Bath Center - South Amir" }]

Find stores with no discounts on specific categories

To find stores without promotions on Smoked Salmon and Anklets, run a query using $nin on the nested categoryName field.

Query:

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

Output:

[{ "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f", "name": "First Up Consultants | Bed and Bath Center - South Amir" }]

Related