![]() |
VOOZH | about |
MongoDB comparison query operators filter documents by matching exact values or selecting value ranges across numeric, string, and date fields, helping retrieve relevant results efficiently.
Examples using a sample "Students" collection in a database named "GeeksforGeeks".
$nin matches none of the values specified in an array.
Example: Retrieving only those Student's documents whose name is not "Alen" or "Clevin".
db.Students.find({name: {$nin: ["Alen", "Clevin"]}})Output:
$in matches any of the values specified in an array.
Example: Retrieving only those students documents whose name is either "Alen" or "Clevin".
db.Students.find({name: {$in: ["Alen", "Clevin"]}})Output:
$lt matches values of the fields that are less than a specified value.
Example: Retrieving those documents where the value of the salary field is less than 55000.
db.Students.find({salary: {$lt: 55000}})Output:
$eq matches the values of the fields that are equal to a specified value.
Example: Retrieving those documents where the value of the branch field is equal to "CSE".
db.Students.find({branch: {$eq: "CSE"}})Output:
$ne matches all values of the field that are not equal to a specified value.
Example: Retrieving those documents where the value of the branch field is not equal to CSE.
db.Students.find({branch: {$ne: "CSE"}})Output:
$gt matches values of the fields that are greater than a specified value.
Example: Retrieving those documents where the value of the salary field is greater than 5000.
db.Students.find({salary: {$gt: 5000}})Output:
$gte matches values of the fields that are greater than or equal to the specified value.
Example: Retrieving those documents where the value of the joiningYear field is greater than equals to 2023.
db.Students.find({joiningYear: {$gte: 2023}})Output:
$lte matches values of the fields that are less than or equal to the specified value.
Example: Retrieving those documents where the value of the joiningYear field is less than or equal to 2023.
db.Students.find({joiningYear: {$lte: 2023}})Output: