142
MongoDB $ne Operator
What is the $ne operator in MongoDB?
MongoDB provides a variety of comparison query operators. The $ne (Not equal) operator is one of those operators. The $ne (Not equal) operator is used to select the documents where the field’s value does not equal to the given value. You can use this operator in the update, find, like method as per your requirements.
Syntax of the $ne operator:
Examples:
In the following examples, we are working with:
>db.employee.find() { "_id" : 1, "name" : "John", "father_name" : "Thomas", "department" : "Contain Writting", "experience" : 2, "language" : { "C#", "python", "java" }, "mobile_no" : 9856321478, "gender" : "Male", "salary" : 22000, "age" : 26 } { "_id" : 2, "name" : "William", "father_name" : "Rebort", "department" : "Softwre Tester", "experience" : 1, "language" : { "C#", "python" }, "mobile_no" : 7896541478, "gender" : "Male", "salary" : 20000, "age" : 21 } { "_id" : 3, "name" : "Ava", "father_name" : "William", "department" : "Marketing manager", "experience" : 5, "language" : { "C#", "C++", "java" }, "mobile_no" : 8789654178, "gender" : "Female", "salary" : 36500, "age" : 25 } { "_id" : 4, "name" : "Olivia", "father_name" : "Noah", "department" : null, "experience" : 4, "language" : { "C++", "java" }, "mobile_no" : 9045641478, "gender" : "Female", "salary" : 30000, "age" : 27 } { "_id" : 5, "name" : "Elijah", "father_name" : "John", "department" : "HR", "experience" : 0, "language" : { "C++", "HTML", "java" }, "mobile_no" : 6589741230, "gender" : "Male", "salary" : 15000, "age" : 20 } { "_id" : 6, "name" : "John", "father_name" : "Liam", "department" : "Softwre Tester", "experience" : 10, "language" : { "C#", "CSS", "HTML" }, "mobile_no" : 9014536987, "gender" : "Male", "salary" : 55000, "age" : 30 }
Example 1: Using $ne operator
In this example, we are looking for only male employees. In other words, we are looking for documents where gender is not equal to the female.
Output:
{ "_id" : 1, "name" : "John", "father_name" : "Thomas", "department" : "Contain Writting", "experience" : 2, "language" : { "C#", "python", "java" }, "mobile_no" : 9856321478, "gender" : "Male", "salary" : 22000, "age" : 26 } { "_id" : 2, "name" : "William", "father_name" : "Rebort", "department" : "Softwre Tester", "experience" : 1, "language" : { "C#", "python"}, "mobile_no" : 7896541478, "gender" : "Male", "salary" : 20000, "age" : 21 } { "_id" : 5, "name" : "Elijah", "father_name" : "John", "department" : "HR", "experience" : 0, "language" : { "C++", "HTML", "java" }, "mobile_no" : 6589741230, "gender" : "Male", "salary" : 15000, "age" : 20 } { "_id" : 6, "name" : "John", "father_name" : "Liam", "department" : "Softwre Tester", "experience" : 10, "language" : { "C#", "CSS", "HTML" }, "mobile_no" : 9014536987, "gender" : "Male", "salary" : 55000, "age" : 30 }
Example 2: Another example
In this example, we are selecting only those employees whose salary field value is not equal to 25000.
Output:
{ "_id" : 1, "name" : "John", "father_name" : "Thomas", "department" : "Contain Writting", "experience" : 2, "language" : { "C#", "python", "java" }, "mobile_no" : 9856321478, "gender" : "Male", "salary" : 22000, "age" : 26 } { "_id" : 2, "name" : "William", "father_name" : "Rebort", "department" : "Softwre Tester", "experience" : 1, "language" : { "C#", "python"}, "mobile_no" : 7896541478, "gender" : "Male", "salary" : 20000, "age" : 21 } { "_id" : 3, "name" : "Ava", "father_name" : "William", "department" : "Marketing manager", "experience" : 5, "language" : { "C#", "C++", "java" }, "mobile_no" : 8789654178, "gender" : "Female", "salary" : 36500, "age" : 25 } { "_id" : 4, "name" : "Olivia", "father_name" : "Noah", "department" : null, "experience" : 4, "language" : { "C++", "java" }, "mobile_no" : 9045641478, "gender" : "Female", "salary" : 30000, "age" : 27 } { "_id" : 5, "name" : "Elijah", "father_name" : "John", "department" : "HR", "experience" : 0, "language" : { "C++", "HTML", "java" }, "mobile_no" : 6589741230, "gender" : "Male", "salary" : 15000, "age" : 20 } { "_id" : 6, "name" : "John", "father_name" : "Liam", "department" : "Softwre Tester", "experience" : 10, "language" : { "C#", "CSS", "HTML" }, "mobile_no" : 9014536987, "gender" : "Male", "salary" : 55000, "age" : 30 }
Next TopicMongoDB $size Operator