In this guide, we will discuss the $not operator in MongoDB. The $not operator in MongoDB is a logical operator for finding documents that don’t match the specified operator phrase. In MongoDB, the $not operator may be used with read methods like as find(), findOneAndUpdate(), and others.
In this post, I’ll show you how to use the $not operator in MongoDB 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:
{ 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
We will consider a comparison operator here in this example, with the $not operator 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 using a $gt comparison operator:
> 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
}
}
Using the $not Operator with a Regular Expression
In this example, I will use a regular expression with the $not operator in MongoDB. let us look for cats in our 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
}
}
Using the $not Operator with Regex Operator
In this example, I will pair my $not operator query with the regex operator in MongoDB. The functionality is the same as the one in the above heading.
I 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
}
}
Using the $not Operator with Embedded Fields with Boolean Values
In this example, I want to look for all those cats whose childFriendly property is not false (or are child-friendly). childFriendly here is a nested or embedded field with which we will use the $not operator in MongoDB 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 Also:
Conclusion
In this article, we learned how to use the $not operator in MongoDB with different examples.