How to use comparison operators in MongoDB to filter data

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 database
db.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.

The $eq operator

The $eq stands for "equal." As the name implies, it is used to find the value that is equal to the specified value.

//query
db> db.courses.find({"_id": { $eq: 11}}).pretty()
//output
[ { _id: 11, course_name: 'C++', hours: 15 } ]
Example of $eq operator

The $gt operator

The $gt stands for "greater than." As the name implies, it is used to find the value that is greater than the specified value.

//query
db.courses.find({"_id": { $gt: 11}}).pretty()
//output
[ { _id: 12, course_name: 'java', hours: 12 } ]
Example of $gt operator

The $gte operator

The $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.

//query
b.courses.find({"_id": { $gte: 11}}).pretty()
//output
[
{ _id: 11, course_name: 'C++', hours: 15 },
{ _id: 12, course_name: 'java', hours: 12 }
]
Example of $gte operator

The $in operator

As its name implies, it is used to find any values specified in an array.

//query
b.courses.find({"_id": { $in: [10,12]}}).pretty()
//output
[
{ _id: 10, course_name: 'python', hours: 10 },
{ _id: 12, course_name: 'java', hours: 12 }
]
Example of $in operator

The $lt operator

The $lt stands for "less." As the name implies, it is used to find the value that is less than the specified value.

//query
b.courses.find({"_id": { $lt: 11}}).pretty()
//output
[ { _id: 10, course_name: 'python', hours: 10 } ]
Example of $lt operator

The $lte operator

The $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.

//query
db.courses.find({"_id": { $lte: 11}}).pretty()
//output
[
{ _id: 10, course_name: 'python', hours: 10 },
{ _id: 11, course_name: 'C++', hours: 15 }
]
Example of $lte operator

The $ne operator

The $ne stands for "not equal." As the name implies, it is used to find all values that are not equal to a specified value.

//query
db.courses.find({"_id": { $ne: 11}}).pretty()
//output
[
{ _id: 10, course_name: 'python', hours: 10 },
{ _id: 12, course_name: 'java', hours: 12 }
]
Example of $ne operator

The $nin operator

The $nin stands for "not in." As the name implies, it is used to find none of the values specified in an array.

//query
db.courses.find({"_id": { $nin: [11,12]}}).pretty()
//output
[ { _id: 10, course_name: 'python', hours: 10 } ]
Example of $nin operator

Run your queries

You can run all the aforementioned MongoDB queries in the terminal below:

Terminal 1
Terminal
Loading...

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved