EQ and NE in MongoDB
Overview
In this article, we will learn about the topic of Equal Eq in mongoDB and Not Equal Ne in MongoDB. Before getting started with the topic, let us get a short overview of the topic.
MongoDB is a well-known NoSQL database system based on documents, providing flexible and scalable data storage capabilities. Its data model follows a document-oriented approach, where data is stored in a format that resembles JSON documents.
MongoDB has several query operators that are frequently used, such as $eq (equal) and $ne (not equal). The Eq in mongoDB $eq operator selects documents where the field value is equal to a specific value, while the Ne in mongoDB $ne operator selects documents where the field value is not equal to a particular value.
So let us now begin with the main agenda of our article, Equal and Not Equal Query in MongoDB.
$eq- MongoDB Equality Operator
MongoDB, a popular NoSQL database platform, utilizes a document-based system to offer versatile and expandable data storage options. Its data architecture follows a document-oriented method, storing data in a structure similar to JSON documents. There are many different types of comparison operators provided by MongoDB, and one of them is the Eq in mongoDB, which we also called as MongoDB Equality operator $eq. The main purpose of the Eq in mongoDB operator $eq is to find documents where the value of a specified field is equal to a certain value.
Note:
- If the provided value is a document, then the order of the fields in the document is important.
- If we provide an array as the value in the query, in that scenario, those documents which have a field that consists of an element that completely resembles the provided array will be returned by MongoDB.
Syntax
Here's the basic syntax of using the Eq in mongoDB $eq operator:
Explanation:
Here, the field is the name of the field that you want to compare, and the value is the value that you want to compare against. If the value of the field matches the specified value, the query returns the document(s) that meet the criteria.
Parameter
Here's a breakdown of the parameters of the above syntax:
- db.collection: specifies the name of the MongoDB collection where you want to search for documents.
- field: the name of the field you want to compare with a value.
- $eq: the equality operator used to compare the specified field with the value.
- value: the value you want to compare with the specified field.
Return Value
The above syntax will return depending on the method we are using for the query, like if it is a find() method, it will return all the documents that match the specified query criteria, if it is a update() method, it returns the updated document. In the case of the Eq in mongoDB $eq operator, the query will return all documents where the value of the specified field is exactly equal to the specified value.
Examples
Let us look at some examples of Eq in MongoDB operators for more clear understanding.
Suppose you have a School database, in which you have a collection called students with the following documents:
- Database: School
- Collection: students
- Document: Four documents that contain the information of the students in the form of key-value pairs.
Before examples, let us write a query to insert all the above documents inside our mongoDB database.
This command will insert the provided documents into a collection called "students" in the database School. Each document has a unique _id field and contains information such as the student's name, age, class, and the courses they are enrolled in.
Output:
Example - 1:
In this example, we will be selecting those documents, where the age of the student is equal to 15 as follows:
This will return the following documents:
Note that in this case, you could also achieve the same result using the simpler syntax:
This will also return the same output:
This is because MongoDB assumes that you want to use the $eq operator if you provide a single value for a field in a query. However, using the $eq operator explicitly can be useful in more complex queries where you need to combine multiple operators.
Example - 2:
In this example, we will be selecting those documents, where the last name of the student is equal to Kumar, or, to put it another way, we are using dot notation to establish conditions on the field in the embedded document.
This will return the following documents:
Note that in this case, you could also achieve the same result using the simpler syntax:
Example - 3:
In this example, we will be selecting those documents, where the course array of the student consists of Math.
This will return the following documents:
Note that in this case, you could also achieve the same result using the simpler syntax:
Example - 4:
In this example, we will be selecting those documents, where the course array of the student is equal to a given array.
This will return the following documents:
Note that in this case, you could also achieve the same result using the simpler syntax:
$ne- MongoDB Inequality Operator
There are many different types of comparison operators provided by MongoDB, and one of them is the Ne in mongoDB, which we also call as MongoDB Inequality operator $ne. The main purpose of the Ne in mongoDB operator $ne is to find documents where the value of a specified field is not equal to a certain value. This operator also covers documents that lack the specified field. Depending on your needs, you can use this operator in methods like find() and update().
Syntax
Here's the basic syntax of using the Ne in mongoDB $ne operator:
Explanation:
Here, the field is the name of the field that you want to compare, and the value is the value that you want to compare against. If the value of the field does not match the specified value, the query returns the document(s) that does not meet the criteria.
Parameter
Here's a breakdown of the parameters of the above syntax:
- db.collection: specifies the name of the MongoDB collection where you want to search for documents.
- field: the name of the field you want to compare with a value.
- $ne: the inequality operator used to compare the specified field with the value and find documents where the value of a specified field is not equal to a certain value.
- value: the value you want to compare with the specified field.
Return Value
The above syntax will return depending on the method we are using for the query, like if it is a find() method, it will return all the documents that match the specified query criteria, if it is a update() method, it returns the updated document. In the case of the Ne in mongoDB$ne operator, the query will return all documents where the value of the specified field is not equal to the specified value.
Examples
Let us look at some examples of Ne in mongoDB operators for more clear understanding.
Suppose you have a School database, in which you have a collection called students. Let us consider the same documents we have inserted inside the mongodb collection above.
- Database: School
- Collection: students
- Document: Four documents that contain the information of the students in the form of key-value pairs.
Example - 1:
In this example, we will be selecting those documents, where the class of the student is not equal to 8 as follows:
This will return the following documents:
Example - 2:
In this example, we will be selecting those documents, where the first name of the student is not equal to Aryan, or, to put it another way, we are using dot notation to establish conditions on the field in the embedded document.
This will return the following documents:
Example - 3:
In this example, we will be selecting those documents, where the course array of the student is not equal to a given array.
This will return the following documents:
Example - 4:
In this example, we will be updating the class of those documents to 12, where the course array of the student does not consist of Math.
The output of the following documents will be:
Explanation:
As can be seen from the updated output, only the document with _id 2 was updated because its course array did not include "Math". The documents with _id 1, 3, and 4 were not updated because their course arrays include "Math".
FAQs
Q. What is the difference between the $eq and $ne operators in MongoDB?
A. The $eq operator selects documents where the field value is equal to a specific value, while the $ne operator selects documents where the field value is not equal to a particular value.
Q. How can I use the $ne operator in MongoDB?
A. The $ne operator can be used in conjunction with other query operators, such as $or, $and, or $not, to create more complex queries. For example, you could use the $ne operator in a query to find all documents where a field does not equal one value and another field does not equal a different value.
Q. Can I use the $ne operator with arrays in MongoDB?
A. Yes, you can use the $ne operator with arrays in MongoDB. When used with an array, the $ne operator selects documents where the field is an array that does not contain the specified value.
Q. How do I use the $ne operator to select documents where a field is not present in MongoDB?
A. You may use the $exists operator along with the $ne operator to choose documents when a field is absent. For instance, the following query picks up all documents that lack the field field:
The documents where either the field field is not present, or if present, the value of that field is not equal to value will be returned by this query.
Conclusion
In this article, we learned about the Equal and Not Equal Query in MongoDB. Let us recap the points we discussed throughout the article:
- MongoDB has several query operators that are frequently used, such as $eq (equal) and $ne (not equal).
- The $eq operator selects documents where the field value is equal to a specific value.
- The $ne operator selects documents where the field value is not equal to a particular value.
- Important point to be noted in the case of MongoDB equality operator $eq is, if the provided value is a document, we should make sure that the fields must be in order in the document, and if we provide an array as the value in the query, in that scenario, those documents which have a field that consists of an element which completely resembles with the provided array will be returned by MongoDB.
- Then, we discussed the equal operator $eq in detail. We have also seen its syntax, parameters, return value, and a few examples for clear understanding.
- Then, we discussed the inequal operator $ne in detail. We have also seen its syntax, parameters, return value, and a few examples for clear understanding.