MongoDB $nor

Summary: in this tutorial, you’ll learn how to use the MongoDB $nor operator to perform a logical NOR operator.

Introduction to the MongoDB $nor operator

The $nor is a logical query operator that allows you to perform a logical NOR operation on a list of one or more query expressions and selects documents that fail all the query expressions.

The syntax of the $nor operator is as follows:

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

MongoDB $nor operator examples

We’ll use the following products collection:

db.products.insertMany([ { "_id" : 1, "name" : "xPhone", "price" : 799, "releaseDate" : ISODate("2011-05-14T00:00:00Z"), "spec" : { "ram" : 4, "screen" : 6.5, "cpu" : 2.66 }, "color" : [ "white", "black" ], "storage" : [ 64, 128, 256 ] }, { "_id" : 2, "name" : "xTablet", "price" : 899, "releaseDate" : ISODate("2011-09-01T00:00:00Z"), "spec" : { "ram" : 16, "screen" : 9.5, "cpu" : 3.66 }, "color" : [ "white", "black", "purple" ], "storage" : [ 128, 256, 512 ] }, { "_id" : 3, "name" : "SmartTablet", "price" : 899, "releaseDate" : ISODate("2015-01-14T00:00:00Z"), "spec" : { "ram" : 12, "screen" : 9.7, "cpu" : 3.66 }, "color" : [ "blue" ], "storage" : [ 16, 64, 128 ] }, { "_id" : 4, "name" : "SmartPad", "price" : 699, "releaseDate" : ISODate("2020-05-14T00:00:00Z"), "spec" : { "ram" : 8, "screen" : 9.7, "cpu" : 1.66 }, "color" : [ "white", "orange", "gold", "gray" ], "storage" : [ 128, 256, 1024 ] }, { "_id" : 5, "name" : "SmartPhone", "price" : 599, "releaseDate" : ISODate("2022-09-14T00:00:00Z"), "spec" : { "ram" : 4, "screen" : 9.7, "cpu" : 1.66 }, "color" : [ "white", "orange", "gold", "gray" ], "storage" : [ 128, 256 ] }, { "_id" : 6, "name" : "xWidget", "spec" : { "ram" : 64, "screen" : 9.7, "cpu" : 3.66 }, "color" : [ "black" ], "storage" : [ 1024 ] } ])
Code language: JavaScript (javascript)

The following example uses the $nor operator to select documents from the products collection:

db.products.find({ $nor :[ { price: 899}, { color: "gold"} ] }, { name: 1, price: 1, color: 1 })
Code language: PHP (php)

It returns documents where:

  • the value is the price field is not 899
  • and the color array does not have any "gold" element.

including the documents that do not contains these fields.

It returned the followig documents:

{ "_id" : 1, "name" : "xPhone", "price" : 799, "color" : [ "white", "black" ] } { "_id" : 6, "name" : "xWidget", "color" : [ "black" ] }
Code language: JSON / JSON with Comments (json)

Summary

  • Use the MongodB $nor operator to perform a logical NOR operation on a list of query expressions and select documents that fail all the query expressions.
Was this tutorial helpful ?