Comparison Operators

The comparison operators in MongoDB are used to perform value-based comparisons in queries. 

The comparison operators in the MongoDB are shown as below:

Comparison Operator Description Syntax
$eq Matches values that are equal to a specified value. { field: { $eq: value } }
$ne Matches all values that are not equal to a specified value. { field: { $ne: value } }
$lt Matches values that are less than a specified value. { field: { $lt: value } }
$gt Matches values that are greater than a specified value. { field: { $gt: value } }
$lte Matches values that are less than or equal to a specified value. { field: { $lte: value } }
$gte Matches values that are greater than or equal to a specified value. { field: { $gte: value } }
$in Matches any of the values specified in an array. { field: { $in: [<value1>, <value2>, ... ] } }

Example of using $gte Operator

Let’s find out all those records in which the age of the person is equal to or greater than 23.

Query:

db.count_no,find( { "Age": {$gte:23} }, {"_id":0} );

Output:

Greater Than Equal Operator.

Explanation: In the above image we have written a query to return the documents that have the age field greater than or equal to 23 and the id is not shown as we mentioned id:0

Example of Using $gt Operator

Let’s find out those people whose age is greater than 21.

Query:

db.count_no,find( { "Age": {$gt:21} }, {"_id":0} );

Output:

Greater Than Operator in MongoDB

Explanation: In the above query we have used find() to retrieve the documents whose age is strictly greater than 21.

Example of Using $eq Operator

Let’s find out if the person named Krishna is present in our Collections or not.

Query:

db.count_no,find( { "name": {$eq:"Krishna"} }, {"_id":0} );

Output:

Equality Operator in MongoDB.

Explanation: In the above-mentioned query we used the $eq operator to find the document with the name Krishna.

MongoDB Query and Projection Operator

MongoDB query and projection operators allow users to control queries and results. Query operators help to filter data based on specific conditions. E.g., $eq,$and,$exists, etc.

Projection operators in MongoDB control which fields should be shown in the results. E.g., $project, $slice, $concat, etc.

In this article, we will learn about query and projection operators in MongoDB with detailed explanations and examples. Let’s start by learning the Query operators in MongoDB.

Similar Reads

MongoDB Query Operators

Similar to SQL MongoDB have also some operators to operate on data in the collection. MongoDB query operators check the conditions for the given data and logically compare the data with the help of two or more fields in the document....

Demo MongoDB Database

To understand the Query and Projection Operators in MongoDB we need a database on which we will perform operations. So we have created a collection called count_no with the help of the db.createCollection( ) function....

Types of Query Operators in MongoDB

The Query operators in MongoDB can be further classified into 8 more types. The 8 types of Query Operators in MongoDB are:...

1. Comparison Operators

The comparison operators in MongoDB are used to perform value-based comparisons in queries....

2. Logical Operators

The logical operators in MongoDB are used to filter data based on expressions that evaluate to true or false....

3. Array Operators

The Array operators in the MongoDB return the result based on the Multiple conditions specified in the array using the array operators....

4. Evaluation Operators

The evaluation operators in the MongoDB are used to return the documents based on the result of the given expression....

5. Element Operators

The element operators in the MongoDB return the documents in the collection which returns true if the keys match the fields and datatypes....

6. Bitwise Operators

The Bitwise operators in the MongoDB return the documents in the MongoDB mainly on the fields that have numeric values based on their bits similar to other programming languages....

7. Geospatial Operators

The Geospatial operators in the MongoDB are used mainly with the terms that relate to the data which mainly focuses on the directions such as latitude or longitudes....

8. Comment Operators

The $comment operator in MongoDB is used to write the comments along with the query in the MongoDB which is used to easily understand the data....

MongoDB Projection Operator

The Projection Operator in MongoDB allows us to control which fields are included in the results of a Query. With the help of the Projection Operator, the performance of our Query will be enhanced....

Conclusion

Query and Project operators are used in MongoDB, mainly for retrieving the data from the database based on the given condition. The MongoDB operators play a crucial role in performing the operations such as CRUD operations....