Open In App

MongoDB – Inequality Operator $ne

Improve
Improve
Like Article
Like
Save
Share
Report

MongoDB provides different types of comparison operators and inequality or not equals operator( $ne ) is one of them. This operator is used to select those documents where the value of the field does not equal to the given value. It also includes those documents that do not contain the specified field. You can use this operator in methods like find(), update(), etc. as per your requirement.

Syntax:

{field: {$ne: value}}

In the following examples, we are working with:

Database: GeeksforGeeks
Collection: employee
Document: four documents that contain the details of the employees in the form of field-value pairs.

Example #1:
In this example, we are selecting those documents where the value of the experienceYear field is not equal to 2.

 
Example #2:
In this example, we are selecting only those documents where the last name of the employees is not Goyal. Or in other words, in this example, we are specifying conditions on the field in the embedded document using dot notation.

 
Example #3:
In this example, we are selecting those documents where the points array is not equal to the specified array.

 
Example #4:
In this example, we are updating the salary to 55000 of those employees whose department is not HR. Or in other words, set the value of the salary field to 55000 of those documents whose department field value is not equal to HR.


Last Updated : 27 Mar, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads