Related Articles

Related Articles

MongoDB NOR operator ( $nor )
  • Last Updated : 22 Apr, 2020

MongoDB provides different types of logical query operators and $nor operator is one of them. This operator is used to perform logical NOR operation on the array of one or more expressions and select or retrieve only those documents that do not match all the given expression in the array. You can use this operator in methods like find(), update(), etc. according to your requirements.

Syntax:

{ $nor: [ { Expression1 }, { Expression2 }, ...  { ExpressionN } ] }

In the following examples, we are working with:

Database: GeeksforGeeks
Collection: contributor
Document: three documents that contain the details of the contributors in the form of field-value pairs.



Matching values using $nor operator:

In this example, we are retrieving only those employee’s documents whose salary is not 3000 and whose branch is not ECE.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.find({$nor: [{salary: 3000}, {branch: "ECE"}]}).pretty()

chevron_right


Matching values in nested/embedded documents using $nor operator:

In this example, we are retrieving only those employee’s documents whose age is not 24 and whose state is not AP.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.find({$nor: [{"personal.age": 24},
                            {"personal.state": "AP"}]}).pretty()

chevron_right


Matching values in an array using $nor operator:

In this example, we are retrieving only those employee’s documents that does not match the given array.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.find({$nor: [{language: {$in: ["Java", "C++"]}}]}).pretty()

chevron_right



Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :