$not Operator in MongoDB – Ultimate 2023 Guide

The $not operator in MongoDB is a logical operator for finding documents that don’t match the given operator expression. In MongoDB, the $not operator may be used with read methods like find(), findOneAndUpdate(), and others.

In this post, we will guide you on how to use the $not operator and show you several examples so you can better understand the concept.

The $not Operator Syntax in MongoDB

Let us take a quick look at the $not operator syntax in MongoDB.

Syntax:

{ field: { $not: { operator-expression } } }

The $not operator even selects documents that do not contain the given field.

Using the $not Operator in MongoDB

Let us explore a few distinct examples to help us understand how we can make use of the $not operator in the mongo shell.

Using the $not Operator with a Comparison Operator

Let’s use the $not operator with a comparison operator to see how it works in MongoDB.

  • Start up the Mongo server
  • Hoose a database and navigate into it using the use db_name command
  • Let us print all documents inside our cats’ collection of the animalShelter database
> db.cats.find().pretty()

{
        "_id" : ObjectId("602eb6c728ff814b64eb3411"),
        "name" : "Stephanie",
        "age" : 18,
        "breed" : "Persian",
        "personality" : {
                "dogFriendly" : true,
                "childFriendly" : true
        }
}
{
        "_id" : ObjectId("602eb73728ff814b64eb3412"),
        "name" : "Whiskers",
        "age" : 3,
        "breed" : "Scottish Fold",
        "personality" : {
                "dogFriendly" : false,
                "childFriendly" : true
        }
}
{
        "_id" : ObjectId("602eb84628ff814b64eb3413"),
        "name" : "Krissy",
        "age" : 5,
        "breed" : "Turkish Angora",
        "personality" : {
                "dogFriendly" : true,
                "childFriendly" : false
        }
}
{
        "_id" : ObjectId("602ebc5ea78db75e09057cd0"),
        "name" : "Cassy",
        "age" : 15,
        "breed" : "Japanese Angora",
        "personality" : {
                "dogFriendly" : true,
                "childFriendly" : false
        }
}
{
        "_id" : ObjectId("602ebc9da78db75e09057cd1"),
        "name" : "Shane",
        "age" : 10,
        "breed" : "Bobtail",
        "personality" : {
                "dogFriendly" : false,
                "childFriendly" : false
        }
}
{
        "_id" : ObjectId("602ebed7a78db75e09057cd2"),
        "name" : "Phoebe",
        "age" : 20,
        "breed" : "Bobtail",
        "personality" : {
                "dogFriendly" : false,
                "childFriendly" : false
        }
}
  • Using the $not operator in the mongo shell and passing the $gt comparison operator as an operator expression:
> db.cats.find( { age: { $not: { $gt: 12 } } } ).pretty()

{
        "_id" : ObjectId("602eb73728ff814b64eb3412"),
        "name" : "Whiskers",
        "age" : 3,
        "breed" : "Scottish Fold",
        "personality" : {
                "dogFriendly" : false,
                "childFriendly" : true
        }
}
{
        "_id" : ObjectId("602eb84628ff814b64eb3413"),
        "name" : "Krissy",
        "age" : 5,
        "breed" : "Turkish Angora",
        "personality" : {
                "dogFriendly" : true,
                "childFriendly" : false
        }
}
{
        "_id" : ObjectId("602ebc9da78db75e09057cd1"),
        "name" : "Shane",
        "age" : 10,
        "breed" : "Bobtail",
        "personality" : {
                "dogFriendly" : false,
                "childFriendly" : false
        }
}

Generally, the $gt comparison operator returns the value that is greater than the given values, but here since we are using it with $not operator, we only got that document when the age field is less than 12.

Using the $not Operator with a Regular Expression

In this example, let’s use a regular expression with the $not operator to look for cats in our MongoDB collection whose names do not start with a capital S.

> db.cats.find( { "name" : { $not : /^S.*/ } } ).pretty()

{
        "_id" : ObjectId("602eb73728ff814b64eb3412"),
        "name" : "Whiskers",
        "age" : 3,
        "breed" : "Scottish Fold",
        "personality" : {
                "dogFriendly" : false,
                "childFriendly" : true
        }
}
{
        "_id" : ObjectId("602eb84628ff814b64eb3413"),
        "name" : "Krissy",
        "age" : 5,
        "breed" : "Turkish Angora",
        "personality" : {
                "dogFriendly" : true,
                "childFriendly" : false
        }
}
{
        "_id" : ObjectId("602ebc5ea78db75e09057cd0"),
        "name" : "Cassy",
        "age" : 15,
        "breed" : "Japanese Angora",
        "personality" : {
                "dogFriendly" : true,
                "childFriendly" : false
        }
}
{
        "_id" : ObjectId("602ebed7a78db75e09057cd2"),
        "name" : "Phoebe",
        "age" : 20,
        "breed" : "Bobtail",
        "personality" : {
                "dogFriendly" : false,
                "childFriendly" : false
        }
}

See, the documents we got do not have the name field starting with the letter “S”.

Using the $not Operator with Regex Operator

In this example, let’s pair the $not operator query with the regex operator in MongoDB. The functionality is the same as the one in the above example.

We want to look for documents whose name doesn’t start with a capital W this time.

> db.cats.find( { name: { $not: { $regex: "^W.*" } } } ).pretty()
{
        "_id" : ObjectId("602eb6c728ff814b64eb3411"),
        "name" : "Stephanie",
        "age" : 18,
        "breed" : "Persian",
        "personality" : {
                "dogFriendly" : true,
                "childFriendly" : true
        }
}
{
        "_id" : ObjectId("602eb84628ff814b64eb3413"),
        "name" : "Krissy",
        "age" : 5,
        "breed" : "Turkish Angora",
        "personality" : {
                "dogFriendly" : true,
                "childFriendly" : false
        }
}
{
        "_id" : ObjectId("602ebc5ea78db75e09057cd0"),
        "name" : "Cassy",
        "age" : 15,
        "breed" : "Japanese Angora",
        "personality" : {
                "dogFriendly" : true,
                "childFriendly" : false
        }
}
{
        "_id" : ObjectId("602ebc9da78db75e09057cd1"),
        "name" : "Shane",
        "age" : 10,
        "breed" : "Bobtail",
        "personality" : {
                "dogFriendly" : false,
                "childFriendly" : false
        }
}
{
        "_id" : ObjectId("602ebed7a78db75e09057cd2"),
        "name" : "Phoebe",
        "age" : 20,
        "breed" : "Bobtail",
        "personality" : {
                "dogFriendly" : false,
                "childFriendly" : false
        }
}

See, the documents we got do not have the name field starting with the letter “W”.

Using the $not Operator with Embedded Fields containing Boolean Values

In this example, let’s look for all those cats whose childFriendly property is not false (i.e. true). childFriendly here is a nested embedded field with which we will use the $not operator with the $eq operator.

> db.cats.find( { "personality.childFriendly" : { $not : { $eq: false } } } ).pretty()

{
        "_id" : ObjectId("602eb6c728ff814b64eb3411"),
        "name" : "Stephanie",
        "age" : 18,
        "breed" : "Persian",
        "personality" : {
                "dogFriendly" : true,
                "childFriendly" : true
        }
}
{
        "_id" : ObjectId("602eb73728ff814b64eb3412"),
        "name" : "Whiskers",
        "age" : 3,
        "breed" : "Scottish Fold",
        "personality" : {
                "dogFriendly" : false,
                "childFriendly" : true
        }
}

See, the documents we got have the childFriendly field true.

Conclusion

$not operator performs logical NOT operation for a given operator expression. It can select fields and documents independently on the bases of some given operator expression. It can be used with multiple operators to get the required result. In this article, we learned how to use the $not operator with different examples in MongoDB. Hope you find it useful.

Reference

MongoDB Docs

Aneesha S
Aneesha S
Articles: 172