MongoDB Operators
We hope, you have already aware about the greater than, less than operators. MongoDB also provides symbols for the comparison operators.
These are the following symbols for the comparison operators -
Operators | Symbols |
< | $lt |
<= | $lte |
> | $gt |
>= | $gte |
Syntax of $lt
{field: {$lt: value} }
Examples of MongoDB Operators
Suppose, we have following 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
The given statement returns only those students whose age less than 11 years -
db.students.find({"age" : {"$lt" : 11}});
Example of $gt
The given statement returns only those students whose age greater than 9 years -
db.students.find({"age" : {"$gt" : 9}});
Example of $gte
The given statement returns only those students whose age greater than equals to 9 years -
db.students.find({"age" : {"$gte" : 9}});
Example of $lte
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
The given statement returns only those students whose age are between 9 and 11 years.
db.students.find({"age" : {"$gte" : 9, "$lte" : 11}});