$nin Operator in MongoDB: Comprehensive 2021 Guide

In this tutorial, I am going to cover the $nin operator in MongoDB.

MongoDB is powerful and a highly advanced database management system that comes with great features and functionalities. It has even become a favorite of beginner developers or coders.

It is power-packed with a bunch of operators that allow developers to integrate noteworthy features and advanced abilities into their applications. Operators allow custom querying, updating, printing, and changing data in the MongoDB database. With advancing features in this database service, there are a lot of smarter and more useful applications springing up.

It has found fandom even among beginners along with senior developers. While the list of operators is huge, I am here to talk about the $nin operator in MongoDB also known as ‘not in’.

The $nin operator is made to use on arrays and it allows us to match those documents whose array field value is not in the given array. Or, it will match the documents where the given fields do not exist.

In this guide, I will walk you through the $nin operator in MongoDB so you can get a better idea of how you can use it in your applications. So, let’s not kill much time and get started.

The $nin Operator Syntax

Here’s a sneak-peek of the syntax of the $nin operator in MongoDB:

{ field: { $nin: [ value1, value2 ... valueN ]} }

Using the $nin Operator in MongoDB

Let us get started with using the $nin operator in MongoDB by taking a look at various examples.

Matching Values in Arrays with $nin Operator in MongoDB

In this example, I will match those documents whose array field value is not the specified value using the $nin operator in MongoDB.

  • Run the MongoDB service
  • Pick the database you wish to work in and move into it:
show dbs
use dronesWorld
  • Let us take a look at all our documents in the collection first:
> db.drones.find({}).pretty()

{
        "_id" : ObjectId("615f1672a7c6532312e3a41d"),
        "utility" : [
                "Delivery"
        ],
        "onSale" : false,
        "name" : "Aurelize Frinworks Hawk 7",
        "price" : 12000,
        "weight" : "34 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Security",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("615f16eca7c6532312e3a41e"),
        "utility" : [
                "Delivery"
        ],
        "onSale" : false,
        "name" : "Poka Moonskull 2X67",
        "price" : 76400,
        "weight" : "48 kilograms",
        "additionalDetails" : {
                "material" : "nylon",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("615f17a5a7c6532312e3a41f"),
        "utility" : [
                "Monitoring or Inspection"
        ],
        "onSale" : false,
        "name" : "Jade Balestrom RW",
        "price" : 24500,
        "weight" : "27 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("615f1879a7c6532312e3a420"),
        "utility" : [
                "Monitoring or Inspection"
        ],
        "onSale" : false,
        "name" : "VarX Lourra VQ (Limited Edition)",
        "price" : 49500,
        "weight" : "19 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Photography",
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615f1922a7c6532312e3a421"),
        "utility" : [
                "Security"
        ],
        "onSale" : false,
        "name" : "Mellori Styxe 175",
        "price" : 15000,
        "weight" : "10 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
  • Next, we will look for those documents that do not have utility as “Delivery” using the $nin operator:
> db.drones.find({utility: {$nin: ["Delivery"]}}).pretty()

{
        "_id" : ObjectId("615f17a5a7c6532312e3a41f"),
        "utility" : [
                "Monitoring or Inspection"
        ],
        "onSale" : false,
        "name" : "Jade Balestrom RW",
        "price" : 24500,
        "weight" : "27 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("615f1879a7c6532312e3a420"),
        "utility" : [
                "Monitoring or Inspection"
        ],
        "onSale" : false,
        "name" : "VarX Lourra VQ (Limited Edition)",
        "price" : 49500,
        "weight" : "19 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Photography",
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615f1922a7c6532312e3a421"),
        "utility" : [
                "Security"
        ],
        "onSale" : false,
        "name" : "Mellori Styxe 175",
        "price" : 15000,
        "weight" : "10 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}

There we go! The $nin operator has spat out only those documents for us that do not have utility as “Delivery”, which is an array field.

Matching Non-Array Values with $nin Operator in MongoDB

In this example, I will match those documents whose non-array value is not the specified value using the $nin operator in MongoDB.

> db.drones.find({name: {$nin: ["Aurelize Frinworks Hawk 7", "Jade Balestrom RW"]}}).pretty()

{
        "_id" : ObjectId("615f16eca7c6532312e3a41e"),
        "utility" : [
                "Delivery"
        ],
        "onSale" : false,
        "name" : "Poka Moonskull 2X67",
        "price" : 76400,
        "weight" : "48 kilograms",
        "additionalDetails" : {
                "material" : "nylon",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("615f1879a7c6532312e3a420"),
        "utility" : [
                "Monitoring or Inspection"
        ],
        "onSale" : false,
        "name" : "VarX Lourra VQ (Limited Edition)",
        "price" : 49500,
        "weight" : "19 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Photography",
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615f1922a7c6532312e3a421"),
        "utility" : [
                "Security"
        ],
        "onSale" : false,
        "name" : "Mellori Styxe 175",
        "price" : 15000,
        "weight" : "10 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}

Amazing! The $nin operator has successfully resulted in those documents whose non-array fields did not match the value I specified.

Matching Values in Nested Documents with $nin Operator in MongoDB

In this example, I am going to match the given value with nested documents using the $nin operator.

> db.drones.find({"additionalDetails.material": { $nin: ["glass fiber", "carbon fiber"]}}).pretty()

{
        "_id" : ObjectId("615f1672a7c6532312e3a41d"),
        "utility" : [
                "Delivery"
        ],
        "onSale" : false,
        "name" : "Aurelize Frinworks Hawk 7",
        "price" : 12000,
        "weight" : "34 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Security",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("615f16eca7c6532312e3a41e"),
        "utility" : [
                "Delivery"
        ],
        "onSale" : false,
        "name" : "Poka Moonskull 2X67",
        "price" : 76400,
        "weight" : "48 kilograms",
        "additionalDetails" : {
                "material" : "nylon",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("615f17a5a7c6532312e3a41f"),
        "utility" : [
                "Monitoring or Inspection"
        ],
        "onSale" : false,
        "name" : "Jade Balestrom RW",
        "price" : 24500,
        "weight" : "27 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}

Pretty cool! We now have those documents in front of us whose specified nested document values did not match the specified value, thanks to the $nin operator!

Read More: $in Operator in MongoDB

Noteworthy References

MongoDB Docs

Aneesha S
Aneesha S
Articles: 172