MongoDB $nor Operator ($nor)

MongoDB provides a variety of logical query operators. The $nor operator is one of those operators. The $nor operator is used to perform logical "NOR operations" on an array of one or more expressions. In other words, the $nor is a logical query operator that allows the user to perform a logical NOR operation on an array of one or more query expressions. This operator is also used to select or retrieve documents that do not match all of the given expressions in the array. The user can use this operator in methods like find(), update(), etc., as per their requirements.

Syntax:

Examples:

In the following examples, we are working with:

>db.student.find()
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d65f"),
           "name" : "Mick",
           "Course" : "btech",
           "batch_year" : 2018,
           "language" : ["c++", "java", "python"],
           "personal_details" : 
                     {
                      "Father_name" : "Jonny",
                      "phone_no" : 8895321456,
                      "age" : 23,
                      "gender" : "Male",
                      "City" : "NewYork",
                     }            
}
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d691"),
           "name" : "Zoya",
           "Course" : "BCA",
           "batch_year" : 2020,
           "language" : ["C#", "JavaScript"],
           "personal_details" : 
                     {
                      "Father_name" : "Henry",
                      "phone_no" : 9874563698,
                      "age" : 20,
                      "gender" : "Female",
                      "City" : "London",
                     }
}
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d655"),
           "name" : "Jonny",
           "Course" : "MCA",
           "batch_year" : 2019,
           "language" : ["C#", "java", "PHP"],
           "personal_details" : 
                     {
                      "Father_name" : "Thomas",
                      "phone_no" : 7845123698,
                      "age" : 24,
                      "gender" : "Male",
                      "City" : "London",
                     }          
}
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d678"),
           "name" : "Oliver",
           "Course" : "BA",
           "batch_year" : 2017,
           "language" : ["c", "PHP"],
           "personal_details" : 
                     {
                      "Father_name" : "William",
                      "phone_no" : 9997845123,
                      "age" : 25,
                      "gender" : "Male",
                      "City" : "Liverpool",
                     }           
}
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d665"),
           "name" : "Mia",
           "Course" : "btech",
           "batch_year" : 2020,
           "language" : ["HTML", "CSS", "PHP"],
           "personal_details" : 
                     {
                      "Father_name" : "Leo",
                      "phone_no" : 6312547896,
                      "age" : 22,
                      "gender" : "Female",
                      "City" : "Manchester",
                     }           
}

Example 1: Matching values using $nor operator

In this example, we are only retrieving the data of those students whose course is not btech.

Output:

>db.student.find({$nor: [{Course: "btech"}]}).pretty()
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d691"),
           "name" : "Zoya",
           "Course" : "BCA",
           "batch_year" : 2020,
           "language" : ["C#", "JavaScript"],
           "personal_details" : 
                     {
                      "Father_name" : "Henry",
                      "phone_no" : 9874563698,
                      "age" : 20,
                      "gender" : "Female",
                      "City" : "London",
                     }
}
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d655"),
           "name" : "Jonny",
           "Course" : "MCA",
           "batch_year" : 2019,
           "language" : ["C#", "java", "PHP"],
           "personal_details" : 
                     {
                      "Father_name" : "Thomas",
                      "phone_no" : 7845123698,
                      "age" : 24,
                      "gender" : "Male",
                      "City" : "London",
                     }          
}
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d678"),
           "name" : "Oliver",
           "Course" : "BA",
           "batch_year" : 2017,
           "language" : ["c", "PHP"],
           "personal_details" : 
                     {
                      "Father_name" : "William",
                      "phone_no" : 9997845123,
                      "age" : 25,
                      "gender" : "Male",
                      "City" : "Liverpool",
                     }           
}


MongoDB $nor Operator

Example 2: Matching two values using $nor operator

In this example, we are only retrieving the data of those students whose course is not btech and whose batch_year is not 2017.

Output:

>db.student.find({$nor: [{Course: "btech"}, {batch_year: "2017"}]}).pretty()
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d691"),
           "name" : "Zoya",
           "Course" : "BCA",
           "batch_year" : 2020,
           "language" : ["C#", "JavaScript"],
           "personal_details" : 
                     {
                      "Father_name" : "Henry",
                      "phone_no" : 9874563698,
                      "age" : 20,
                      "gender" : "Female",
                      "City" : "London",
                     }
}
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d655"),
           "name" : "Jonny",
           "Course" : "MCA",
           "batch_year" : 2019,
           "language" : ["C#", "java", "PHP"],
           "personal_details" : 
                     {
                      "Father_name" : "Thomas",
                      "phone_no" : 7845123698,
                      "age" : 24,
                      "gender" : "Male",
                      "City" : "London",
                     }          
}

Example 3: Matching values in an array using $nor operator

In this example, we are only retrieving the data of students who do not match the given array.

Output:

> db.student.find({$nor: [{language: {$in: ["Java", "C#"]}}]}).pretty()
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d678"),
           "name" : "Oliver",
           "Course" : "BA",
           "batch_year" : 2017,
           "language" : ["c", "PHP"],
           "personal_details" : 
                     {
                      "Father_name" : "William",
                      "phone_no" : 9997845123,
                      "age" : 25,
                      "gender" : "Male",
                      "City" : "Liverpool",
                     }           
}
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d665"),
           "name" : "Mia",
           "Course" : "btech",
           "batch_year" : 2020,
           "language" : ["HTML", "CSS", "PHP"],
           "personal_details" : 
                     {
                      "Father_name" : "Leo",
                      "phone_no" : 6312547896,
                      "age" : 22,
                      "gender" : "Female",
                      "City" : "Manchester",
                     }           
}

Example 4: MongoDB Logical $nor Operator (retrieving the data in the embedded documents)

In this example, we are only getting the data for students whose city is not "London" and whose is not 22 years old.

Output:

>db.student.find({$nor: [{"personal.age": 22}, {"personal.city": "London"}]}).pretty()
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d65f"),
           "name" : "Mick",
           "Course" : "btech",
           "batch_year" : 2018,
           "language" : ["c++", "java", "python"],
           "personal_details" : 
                     {
                      "Father_name" : "Jonny",
                      "phone_no" : 8895321456,
                      "age" : 23,
                      "gender" : "Male",
                      "City" : "NewYork",
                     }            
}
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d678"),
           "name" : "Oliver",
           "Course" : "BA",
           "batch_year" : 2017,
           "language" : ["c", "PHP"],
           "personal_details" : 
                     {
                      "Father_name" : "William",
                      "phone_no" : 9997845123,
                      "age" : 25,
                      "gender" : "Male",
                      "City" : "Liverpool",
                     }           
}

Next Topic#




Latest Courses