Create a Collection
Starting with the illustrations of MongoDB’s $not and $ne operators, we create a collection named “new” and add 10 records to it. All the records contain their unique identifier, the name, and the score field as per the output of the “find” query that is used in the following shown image. There are also some duplicate values in the “name” and “score” fields of the “new” collection which will be helpful in our code examples.
Example 1: NOT Operator
As we all know, MongoDB’s $not logical operator is specific for denying any condition that is mentioned in the query. Therefore, we start with the essential query example of using the $not operator that is applied to the “new” collection. We use the $lt (less than) operator on the “score” field to only get the records of the collection where the “score” field has values that are less than 50. While the $lt operator gets the less than 50 value of the “score” field, the $not operator negates it by doing the opposite.
Therefore, the find() function now displays all the records of the “new” collection where the “score” field has values that are greater than “50”. The output image illustrates the use of the $not operator by displaying a total of five records from the “new” collection where no “score” field has a value that is less than “50”.
Example 2: NOT Operator to Neglect a Certain Condition
Remember that the $not operator can only be applied to a condition within the query and you cannot apply it to a whole expression. To understand it well, we use the $not operator on a whole expression to negate it as per the following mentioned MongoDB find() function command. The output for this query is attached in the image which shows that MongoDB throws an exception. For example, to negate the full expression, use the “$nor” operator.
This proves that we need to create a condition within the command to use the $not operator on it. Thus, we update the query and add the “eq” operator within the query that searches for the records where a student’s name should be “Cillian”.
After that, the $not operator is applied on top of it to negate the $eq operator condition. This does the opposite and displays the records where the name field has no value that matches“Cillian”. You can see that all the records from the “new” collection are for other students except “Cillian”. The record for “Cillian” is displayed because the MongoDB queries are case sensitive.
Example 3: Not Equal Operator
While the $not operator is utilized to negate a whole condition, the $ne (not equal) operator is a condition that checks if the specific field value is not equal to a specified certain value. Thus, it returns the results according to the situation.
To illustrate its working, we utilize the find() function command in MongoDB where the “$ne” operator takes the “Bryan” value to check it in the “name” field of the collection. If any of the records contains a value other than “Bryan” within the “name” field, it returns those records and display them. This means that the records with “name” set to “Bryan” are discarded from the output.
The output of this query exactly displayis the same thing that we discussed just now. All the records are different from each other, while there is no record with the name “Bryan”. That is how the $ne operator discards specific field values while it is used in the find() function query.
Example 4: Not Equal Operator to Negate a Matching Record
The MongoDB $ne operator can be utilized more than once in a query because it is a negating condition itself. Also, we can apply other logical operators to it like $and, $or, and $nor. Assume that we want to discard multiple dissimilar records from the “new” collection and utilize the $ne operator. So, we generate a query where we set the total of three conditions on the “name” field of the collection separately using the $ne logical operator. This operator searches all the records from the “new” collection only where the “name” of a specific person is not “John”, “Bryan”, and “Cillian”. All the other records are fetched.
Note that all the conditions contain each other’s same “name” field records. After this, we apply the $AND operator on all three conditions. The use of the $AND operator only produces the result with the common records that are generated from these conditions. It works like an intersection when all conditions are true. The first condition where the value is specified as “John” also generates the record for the names “Cillian” and “Bryan” and vice versa. The other two conditions also have records for the other conditions.
So, the records for “John”, “Bryan”, and “Cillian” are discarded. We are left with a total of five records for the names “Cillian”, “Ana”, “Peter”, and “Nina”.
Conclusion
The purpose of this guide is to demonstrate the difference between two logical operators of MongoDB – not and not equal. The query illustrations provided in this article exhibit the differences and similarities of using both logical operators on query conditions while not negating their actual same purpose to neglect a certain condition via the $not or negate a matching record via the $ne.