Updated March 8, 2023
Introduction to Mongodb not equal
MongoDB provides a different kind of comparison operator to the user, the not equal is one of the comparison operators that are provided by MongoDB. Normally MongoDB “not equal” a comparison operator, it is used to compare the two different values such as specified value and filed value. Basically, an equal operator is used to fetch the required document where the value of a field from the collection is not equal to the specified value. not equal operators we can use different methods such as like, find and update as per our requirement. The MongoDB “not equal” comparison operator we can denote by using the $ne symbol.
Syntax:
{specified field name:{$ne: specified value}}
Explanation
In the above syntax, we use the comparison operator of MongoDB that is not equal ($ne) with two different parameters as follows.
specified field name: it is used to field name from the collection and this field is dependent on the user.
$ne: it is the symbol of not equal comparison operator
specified value: It is used to define the value for the comparison
How not equal works in Mongodb?
Now let’s see how the not equal comparison operator works in MongoDB as follows.
First, try to understand what the operator is. MongoDB provides different kinds of operators such as comparison, logical, element array and bitwise, etc. But we commonly used comparison operators among the different operators. Again we have different types of comparison operators such as eq, gt, lt, and gte, etc, and this operator we can use as per our requirement.
Normally the “not equal” operator is used to compare the specified value with all documents from the collection and if it finds any matching document from the collection then it displays all documents from the collection.
The working of the “not equal” operator is simple; we just need to mention the specified field name with the specified value that we want. The specified field name and specified value depend on the user. At the time of execution, it compares all specified values with all documents from the collection and if the value of the document is not equal to the specified value then it displays specified records.
Example
Now let’s see the different examples of not equal operators as follows.
First, we need to create the new collection as follows.
First, we need to use a database then create a collection as follows.
First, we created a sample database by using the following statement as follows.
use sample
Explanation
In the above statement, we use the command to create the new database, here we successfully created a sample database and we use it. The end result or we can say the output of the above statement we illustrated by using the following screenshot as follows.
After successful creation of the database, we need to create the collection by using the following statement as follows.
db.createCollection("student_info")
Explanation
In the above statement, we use createCollection command to create the new collection; here we created a new collection name as a student_info as shown. The end result or we can say the output of the above statement we illustrated by using the following screenshot as follows.
Now we have a collection, so now we need to insert the same document into the newly created collection that is a student_info by using the following statement as follows.
db.student_info.insert({ name: "Johan", dept: "IT", marks: 75, dbsm:56})
db.student_info.insert({ name: "Sameer", dept: "comp", marks: 12, dbsm:45})
db.student_info.insert({ name: "pooja", dept: "comp", marks: 45, dbsm:65})
db.student_info.insert({ name: "Rohit", dept: "mech", marks: 71, dbsm:87})
db.student_info.insert({ name: "Sachin", dept: "IT", marks: 98, dbsm:74})
Explanation
By using the above statement we successfully inserted five documents into the student_info collection, in which we inserted the name of the student, department name of the student, marks of a student, and marks of the DBMS subject as shown.
Now we can see all documents from the collection by using the following statement as follows.
db.student_info.find()
Explanation
The end result or we can say the output of the above statement we illustrated by using the following screenshot as follows.
Now we can implement the not equal comparison operator as follows.
db.student_info.find({marks:{$ne:45}})
Explanation
In the above example, we try to find out that student marks are not equal to the 45, at that time we can use the above statement. . Here we first specify the collection name that is a student after that we use the find function with marks filed and user-specified value as shown. The end result or we can say the output of the above statement we illustrated by using the following screenshot as follows.
Now let’s see more examples of not equal for better understanding as follows.
db.student_info.find({marks:{$ne:12}})
Explanation
Similarly, like the above example, we need to find all documents whose marks are not equal to 12. Here we first specify the collection name that is a student after that we use the find function with marks filed and user-specified value as shown. The end result or we can say the output of the above statement we illustrated by using the following screenshot as follows.
Now let’s see how we can use the update method with the $ne comparison operator as follows.
db.student_info.update( { "marks": { $ne: 12 } }, { $set: { dbms: 80 } } )
Explanation
In the above example, we try to implement the not equal with update method as shown. Here we need to set 80 marks for the DBMS subject if student marks are not equal to 12. The end result or we can say the output of the above statement we illustrated by using the following screenshot as follows.
Now we can use the find method to see the updated documents as follows.
db.student_info.find()
Explanation
The end result or we can say the output of the above statement we illustrated by using the following screenshot as follows.
Similarly, we can use “not equal” comparison operators with the different methods as per our requirement.
Conclusion
We hope from this article you learn more about MongoDB not equal. From the above article, we have learned the basic syntax of the not equal and we also see different examples of the not equal. From this article, we learned how and when we use MongoDB not equal.
Recommended Articles
This is a guide to Mongodb not equal. Here we discuss the basic syntax of the not equal and we also see different examples of the not equal. You may also have a look at the following articles to learn more –