MongoDB $nor Operator – A Brief Overview

You know many operators that can be used to filter the selection criteria of documents from a MongoDB collection. Those operators return documents that meet specified parameters. What if we want to exclude certain criteria? Well, MongoDB has various operators for this too. One of them that we will talk about in this article is the $nor operator.

In this article, we will learn about the $nor operator and its syntax, and demonstrate it with different operators for various use cases. Let’s get started.

MongoDB $nor Operator

$nor applies a logical NOR operation on an array of one or more query expressions and selects documents that fail any of the query expressions in the array. In simple words, it keeps out all those documents that match at least one of the expressions or conditions.

Syntax:

Below is the syntax showing how we can use the $nor operator in MongoDB.

db.collection.find({ $nor: [ { expression1 }, { expression2 }, ...  { expressionN } ] })

Parameters:

  • “collection” is the name of the collection where we are looking for the document,
  • “find” is the method we are using to query the documents,
  • expression” contains criteria that should not be present in the selection.

Using the $nor Operator in MongoDB

Let us get started with using the $nor operator in MongoDB by specifying different types of queries.

Setup:

1. Start the MongoDB shell.

2. Execute the below command to list all the available databases.

show dbs

3. Use the below command to move into one of them.

use droneShop

Here we are using the “droneShop” database where we have already created a collection “drones”.

Following are the documents inside “drones”:

> db.drones.find().pretty()
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0c"),
        "onSale" : false,
        "name" : "Nimbari Gryphon Medeta 65",
        "price" : 77500,
        "weight" : "77 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Cinematography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0d"),
        "onSale" : false,
        "name" : "X-Strimmer Eye",
        "price" : 23500,
        "weight" : "24 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Precision Agriculture",
                        "Cinematography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0e"),
        "onSale" : false,
        "name" : "Khai Balemosh Shefqa TRX",
        "price" : 120500,
        "weight" : "80 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0f"),
        "onSale" : false,
        "name" : "Sifinist Croma AX",
        "price" : 99500,
        "weight" : "97 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf10"),
        "onSale" : false,
        "name" : "Drovce Finnifield FR-7",
        "price" : 87600,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("6195514a50f8bacf51bdb6ac"),
        "onSale" : true,
        "name" : "PlayGarra 2078-56",
        "price" : 15000,
        "weight" : "5 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Cinematography",
                        "Wildlife Watching"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("6195514a50f8bacf51bdb6ad"),
        "onSale" : true,
        "name" : "Frinty Gemini 3X70",
        "price" : 55000,
        "weight" : "22 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Cinematography",
                        "Wildlife Watching",
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("6195514a50f8bacf51bdb6ae"),
        "onSale" : true,
        "name" : "Rilche Gabbana Flier RG950",
        "price" : 110000,
        "weight" : "100 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Cinematography",
                        "Wildlife Watching",
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}

We have a total of 8 documents in the “drones” collection.

If you are wondering why we have used  pretty() function, it is used here to format the output with a proper indentation and increase readability making documents easier to view and understand.

Using the $nor Operation with Two Expressions in MongoDB

In this example, we will use the $nor operator with two expressions. If documents satisfy any of the two expressions they will not be included in the results.

Example:

Let’s try to look for all those documents that do not have the price of 110000 and are not even on sale.

> db.drones.find( { $nor: [ { price: 110000 }, { onSale: true } ]  } ).pretty()
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0c"),
        "onSale" : false,
        "name" : "Nimbari Gryphon Medeta 65",
        "price" : 77500,
        "weight" : "77 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Cinematography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0d"),
        "onSale" : false,
        "name" : "X-Strimmer Eye",
        "price" : 23500,
        "weight" : "24 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Precision Agriculture",
                        "Cinematography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0e"),
        "onSale" : false,
        "name" : "Khai Balemosh Shefqa TRX",
        "price" : 120500,
        "weight" : "80 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0f"),
        "onSale" : false,
        "name" : "Sifinist Croma AX",
        "price" : 99500,
        "weight" : "97 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf10"),
        "onSale" : false,
        "name" : "Drovce Finnifield FR-7",
        "price" : 87600,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}

We have found all documents where either the “price” field is not equal to 110000 or the “onSale” field is not true.

Using the $nor Operator with a Comparison Operator in MongoDB

In this example, we will use the $nor operator with a comparison operator. We’ll be using the $lte operator.

Example:

Let’s try to get all those documents that do not have prices less than or equal to 60,000 and are not even on sale.

> db.drones.find( { $nor: [ { price: { $lte: 60000 } }, { onSale: true } ] } ).pretty()
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0c"),
        "onSale" : false,
        "name" : "Nimbari Gryphon Medeta 65",
        "price" : 77500,
        "weight" : "77 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Cinematography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0e"),
        "onSale" : false,
        "name" : "Khai Balemosh Shefqa TRX",
        "price" : 120500,
        "weight" : "80 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0f"),
        "onSale" : false,
        "name" : "Sifinist Croma AX",
        "price" : 99500,
        "weight" : "97 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf10"),
        "onSale" : false,
        "name" : "Drovce Finnifield FR-7",
        "price" : 87600,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}

We found 4 documents where either the “price” field has a value that is not less than or equal to 60,000 or the “onSale” field is not true.

Using the $nor Operator on an Embedded Document

In this example, we will use the $nor operator on an embedded field.

Example:

Let’s try to look for all those documents that do not have the value of the embedded document “material” as “glass fibre” and are on sale.

> db.drones.find( { $nor: [ { "additionalDetails.material": "glass fibre" }, { onSale: false } ] } ).pretty()
{
        "_id" : ObjectId("6195514a50f8bacf51bdb6ac"),
        "onSale" : true,
        "name" : "PlayGarra 2078-56",
        "price" : 15000,
        "weight" : "5 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Cinematography",
                        "Wildlife Watching"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("6195514a50f8bacf51bdb6ad"),
        "onSale" : true,
        "name" : "Frinty Gemini 3X70",
        "price" : 55000,
        "weight" : "22 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Cinematography",
                        "Wildlife Watching",
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("6195514a50f8bacf51bdb6ae"),
        "onSale" : true,
        "name" : "Rilche Gabbana Flier RG950",
        "price" : 110000,
        "weight" : "100 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Cinematography",
                        "Wildlife Watching",
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}

We found 3 documents matching the query.

Using the $nor Operator on an Embedded Array Field

In this example, we will be using the $nor operator on an embedded array field.

Example:

Let’s try to get all those documents that do not have the given nested array field value and are not even on sale.

> db.drones.find( { $nor: [ { "additionalDetails.moreUses": [
...                         "Cinematography",
...                         "Wildlife Watching",
...                         "Precision Agriculture",
...                         "Land Inspection",
...                         "Water Inspection",
...                         "Videography"
...                 ] }, { onSale: true } ] } ).pretty()


{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0c"),
        "onSale" : false,
        "name" : "Nimbari Gryphon Medeta 65",
        "price" : 77500,
        "weight" : "77 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Cinematography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0d"),
        "onSale" : false,
        "name" : "X-Strimmer Eye",
        "price" : 23500,
        "weight" : "24 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Precision Agriculture",
                        "Cinematography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0e"),
        "onSale" : false,
        "name" : "Khai Balemosh Shefqa TRX",
        "price" : 120500,
        "weight" : "80 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0f"),
        "onSale" : false,
        "name" : "Sifinist Croma AX",
        "price" : 99500,
        "weight" : "97 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf10"),
        "onSale" : false,
        "name" : "Drovce Finnifield FR-7",
        "price" : 87600,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}

As a result, we found 5 documents for this query.

Conclusion

The $nor in MongoDB is a logical query operator. This operator is used to perform a logical NOR operation. It takes an array of expressions and selects the documents that fail to satisfy any of those expressions. We can use this operator with various other operators, even on embedded documents and embedded array fields. We hope that after reading this article you will get answers to all your questions.

Reference

$nor — MongoDB Manual

Aneesha S
Aneesha S
Articles: 171