MongoDB Comparison Operators
MongoDB offers various kinds of operators that can be utilized to interact with the database. In computer programming, operators are symbols or keywords that usually represents an action or process. It informs a compiler or an interpreter to carry out mathematical or logical operations.
Like other programming language, MongoDB also provides differernt types of operators. In this post, we have mentioned about the comparison operators.
MongoDB Comparison Operators
These are the following symbols for the comparison operators to compare the values of the documents.
Operators | Symbols | Descriptions |
< | $lt | It is used to match values of the fields that are less than a specified value. |
<= | $lte | It is used to match values of the fields that are less than equals to the specified value. |
> | $gt | It is used to match values of the fields that are greater than a specified value. |
>= | $gte | It is used to match values of the fields that are greater than equal to the specified value. |
== | $eq | It is used to match values that are equal to the given value. |
(Available in) | $in | It matches any of the values in an array. |
!= | $ne | It matches values that are not equal to the given value. |
(Not available in) | $nin | It matches none of the values specified in an array. |
Syntax of $lt
{field: {$lt: value} }
Examples of MongoDB Operators
Suppose, we have the following 'students' collection.
{ "_id" : ObjectId("5b7ddc4f529cbc23546dc4c7"), "name" : "Jorz Rai", "age" : 10, "class" : "5A" }
{ "_id" : ObjectId("5b7ddf10529cbc23546dc4c8"), "name" : "Rana Soi", "age" : 11, "class" : "5C" }
{ "_id" : ObjectId("5b7de0f4529cbc23546dc4c9"), "name" : "Andy Joya", "age" : 11, "class" : "5C" }
{ "_id" : ObjectId("5b7de0f4529cbc23546dc4ca"), "name" : "Mary Soi", "age" : 11, "class" : "5C" }
{ "_id" : ObjectId("5b7de0f4529cbc23546dc4cb"), "name" : "Priska Somya", "age" : 12, "class" : "6A" }
Example of $lt operator
The given statement returns only those students whose age less than 11 years -
db.students.find({"age" : {"$lt" : 11}});

Example of $gt operator
The given statement returns only those students whose age greater than 9 years -
db.students.find({"age" : {"$gt" : 9}});

Example of $gte operator
The given statement returns only those students whose age greater than equals to 9 years -
db.students.find({"age" : {"$gte" : 9}});

Example of $lte operator
The given statement returns only those students whose age less than equals to 11 years -
db.students.find({"age" : {"$lte" : 11}});

Example of $gte and $lte operators
The given statement returns only those students whose age are between 9 and 11 years.
db.students.find({"age" : {"$gte" : 9, "$lte" : 11}});

Example of $in operator
The given statement returns only those students whose age are either 10 or 12 years.
db.students.find({"age" : {"$in" : [ 10, 12]}});

Example of $ne operator
The given statement returns only those students who are not in class 5C.
db.students.find({"class" : {"$ne" : "5C"}});

Example of $nin operator
The given statement returns only those students who are none of the values class 5C.
db.students.find({"class" : {"$nin" : ["5C","5A"]}});
