Logical Operators in MongoDB – Quick Guide

In this tutorial, I am going to explain in detail all the logical operators in MongoDB.

Logical operators in MongoDB allow developers/users to apply different types of conditions allowed by each of them. There are in total 4 logical operators available to us, all of which we will discuss.

Here’s a sneak peek at the list of logical operators available in MongoDB:

$and joins query clauses with a logical AND operator and returns all documents matching the conditions of both clauses.
$not The impact of a query expression is reversed, and documents that do not match the query phrase are returned.
$nor When query clauses are joined with a logical NOR, all documents that fail to match both clauses are returned.
$or Joins query clauses using a logical OR and returns all documents that meet the requirements of either clause.

This guide will cover examples for each of the logical operators so you can understand how to make use of each of them to fulfill your purposes.

Let us get started with our detailed guide on all the logical operators!

Logical Operators Syntaxes

Let us take a quick look at the syntaxes of all the logical operators in MongoDB.

$and Operator

{ $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ]

$not Operator

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

$nor Operator

{ $nor: [ { <expression1> }, { <expression2> }, ...  { <expressionN> } ] }

$or Operator

{ $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }

Perfect! Let us now start using these logical operators in one of our databases and understand how each of them work.

Using All the Logical Operators – One-by-One

Let us look at a few demonstrations on using each of the logical operators in our cats collection in our animalShelter database.

Well, before we start, let us take a look at all our available documents on which we will perform operations of all the logical operators.

> 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
        }
}

We have in total 6 cats in our collection.

Using the $and Logical Operator in MongoDB

The $and operator in MongoDB performs one or more expressions that we pass inside an array. It also selects documents that satisfy all of the given expressions inside our query.

Let us look for cats who are less than age 15 and are dog friendly.

> db.cats.find({ $and : [ { age : { $lt: 15 } }, { "personality.dogFriendly" : { $eq : true } } ] } ).pretty()

{
        "_id" : ObjectId("602eb84628ff814b64eb3413"),
        "name" : "Krissy",
        "age" : 5,
        "breed" : "Turkish Angora",
        "personality" : {
                "dogFriendly" : true,
                "childFriendly" : false
        }
}

We have successfully found 1 cat that meets our query.

Using the $not Logical Operator in MongoDB

The $not logical operator looks for all those documents in our collection that do not meet the given expression. This also includes those documents that do not contain a specific field.

Let us look for all those cats who are not child-friendly.

> db.cats.find( { "personality.childFriendly" : { $not: { $eq: true } } } ).pretty()
{
        "_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
        }
}

Looks like 4 of our cats are not child-friendly :P.

Using the $nor Logical Operator in MongoDB

The $nor logical operator in MongoDB accepts one or more expressions inside an array for query and spits out those documents that fail to meet all the specified expressions.

Let us look for all those cats whose name is not Shane, they are not aged 10, and do not belong to the Turkish Angora breed.

> db.cats.find( { $nor: [ { name: "Shane" }, { age: 10 },{ breed: "Turkish Angora" } ]  } ).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("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
        }
}

Awesome! We have successfully printed all the cats that did not match all the expressions in our query.

Using the $or Logical Operator in MongoDB

The $or logical operator in MongoDB accepts an array of expressions and find those documents that satisfy at least one of the specified arrays.

Let us find cats that are either not dog friendly or child friendly.

> db.cats.find( { $or: [ { "personality.dogFriendly" : { $eq: false } }, { "personality.childFriendly" : false } ] } ).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("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
        }
}

Awesome! We have successfully found documents that meet our query.

This brings an end to our guide on logical operators. Hope it was helpful!

Read in detail about:

$or Operator

$not Operator

Conclusion

In this article, we learned to use all the logical operators in MongoDB with this detailed guide.

Noteworthy References

MongoDB Docs

Aneesha S
Aneesha S
Articles: 171