Document-oriented database, MongoDB is a type of NoSQL database. Different types of operators are available in MongoDB for usage when interacting with the database.
Operators are specialized symbols or keywords that mainly tell a compiler or an interpreter how to perform logical or mathematical operations. This answer provides a thorough explanation of the comparison operators in MongoDB.
We'll use the following database to perform different comparison operators where the database name is educative and has a collection named courses.
use educative //selecting our databasedb.courses.find({}) //query//output[{ _id: 10, course_name: 'python', hours: 10 },{ _id: 11, course_name: 'C++', hours: 15 },{ _id: 12, course_name: 'java', hours: 12 }]
Note: To learn about element operators in MongoDB, click here.
$eq
operatorThe $eq
stands for "equal." As the name implies, it is used to find the value that is equal to the specified value.
//querydb> db.courses.find({"_id": { $eq: 11}}).pretty()//output[ { _id: 11, course_name: 'C++', hours: 15 } ]
$gt
operatorThe $gt
stands for "greater than." As the name implies, it is used to find the value that is greater than the specified value.
//querydb.courses.find({"_id": { $gt: 11}}).pretty()//output[ { _id: 12, course_name: 'java', hours: 12 } ]
$gte
operatorThe $gte
stands for "greater than equal." As the name implies, it is used to find the value that is greater or equal to the specified value.
//queryb.courses.find({"_id": { $gte: 11}}).pretty()//output[{ _id: 11, course_name: 'C++', hours: 15 },{ _id: 12, course_name: 'java', hours: 12 }]
$in
operatorAs its name implies, it is used to find any values specified in an array.
//queryb.courses.find({"_id": { $in: [10,12]}}).pretty()//output[{ _id: 10, course_name: 'python', hours: 10 },{ _id: 12, course_name: 'java', hours: 12 }]
$lt
operatorThe $lt
stands for "less." As the name implies, it is used to find the value that is less than the specified value.
//queryb.courses.find({"_id": { $lt: 11}}).pretty()//output[ { _id: 10, course_name: 'python', hours: 10 } ]
$lte
operatorThe $lte
stands for "less than equal." As the name implies, it is used to find the value that is less than or equal to the specified value.
//querydb.courses.find({"_id": { $lte: 11}}).pretty()//output[{ _id: 10, course_name: 'python', hours: 10 },{ _id: 11, course_name: 'C++', hours: 15 }]
$ne
operatorThe $ne
stands for "not equal." As the name implies, it is used to find all values that are not equal to a specified value.
//querydb.courses.find({"_id": { $ne: 11}}).pretty()//output[{ _id: 10, course_name: 'python', hours: 10 },{ _id: 12, course_name: 'java', hours: 12 }]
$nin
operatorThe $nin
stands for "not in." As the name implies, it is used to find none of the values specified in an array.
//querydb.courses.find({"_id": { $nin: [11,12]}}).pretty()//output[ { _id: 10, course_name: 'python', hours: 10 } ]
You can run all the aforementioned MongoDB queries in the terminal below:
Free Resources