MongoDB $not Operator

MongoDB provides a variety of logical query operators. The $not operator is one of those operators. It is similar to the Logical NOT operator in programming. The $not operator is used to perform logical "NOT operation" on the specified expressions. You can use it with any other query expression, such as equal to ($eq), greater than ($gt), or less than ($lt).

The $not operator can be used with regular expressions. It selects or retrieves only those documents that do not match the given operator expression. 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:

Example 1: MongoDB Logical $not Operator (at least):

In this example, we are only retrieving the data of students whose "Total_marks" is at least 400.

SQL equivalent command:

Output:

>db.student.find({"Total_marks" : {$not: {$lt : 400}}}).pretty()
{      
                "_Id" : ObjectId("4565d4cd85d4f1vf6345612"),    
                "std_name" : "Jenny",
                "sex" : "Female",
                "class" : "VI",
                "age" : 13,
                "Total_marks" : 800
                "Result" : "Pass"
}
{
                "_Id" : ObjectId("4565d4cd85d4f1vf6345665"),
                "std_name" : "Lassy",
                "sex" : "Female",
                "class" : "X",
                "age" : 17,
                "Total_marks" : 550
                "Result" : "Pass"
}
{
                "_Id" : ObjectId("4565d4cd85d4f1vf6345756"),
                "std_name" : "Mike,
                "sex" : "Male",
                "class" : "V",
                "age" : 16,
                "Total_marks" : 700
                "Result" : "Pass"
}

Example 2: MongoDB Logical $not Operator (not greater than):

In this example, we are only retrieving data of students whose "age" is not greater than 12.

Output:

>db.student.find({"age" : {$not: {$gt : 12}}}).pretty()
{
                "_Id" : ObjectId("4565d4cd85d4f545ffg43df7"),
                "std_name" : "Jack",
                "sex" : "Male",
                "class" : "VI",
                "age" : 11,
                "Total_marks" : 303
                "Result" : "Pass"
} 
{
                "_Id" : ObjectId("4565d4cd85d4f545ffg43df7"),
                "std_name" : "Thomas",
                "sex" : "Male",
                "class" : "V",
                "age" : 11,
                " Total_marks" : 200
                " Result" : "Fail"
}

Example 3: MongoDB Logical $not Operator (not equal to):

In this example, we are only retrieving data of students whose "age" is not equal to 11.

Output:

>db.student.find({"age" : {$not: {$eq : 11}}}).pretty()
{
               "_Id" : ObjectId("4565d4cd85d45d4dgf1fd5f4"),
               "std_name" : "Jenny",
                "sex" : "Female",
                "class" : "VI",
                "age" : 13,
                "Total_marks" : 800
                "Result" : "Pass"
}
{
                "_Id" : ObjectId("4565d4cd85d45d4dgf1fd55s"),
                "std_name" : "Lassy",
                "sex" : "Female",
                "class" : "X",
                "age" : 17,
                "Total_marks" : 550
                "Result" : "Pass"
}
{
                "_Id" : ObjectId("4565d4cd85d45d4d85331531"),
                "std_name" : "Mia",
                "sex" : "Female",
                "class" : "X",
                "age" : 19,
                " Total_marks" : 350
                "Result" : "Pass"
}
{
                "_Id" : ObjectId("4565d4cd85d45d4456c53154"),
                "std_name" : "Mike,
                "sex" : "Male",
                "class" : "V",
                "age" : 16,
                "Total_marks" : 700
                "Result" : "Pass"
}	





Latest Courses