Greater than filter query
To get the specific numeric data using conditions like greater than equal or less than equal use the $gte or $lte operator in the find() method.
Syntax:
db.collection_name.find({< key > : {$gte : < value >}})
or
db.collection_name.find({< key > : {$lte : < value >}})
Example:
Retrieve and display all documents from the ‘article’ collection where the length is greater than or equal to 510.
db.article.find({length:{$gte:510}}).pretty()
Output:
[
{
_id: ObjectId('60095b8a3fc110f90873ce2c'),
title: 'Tree',
author: 'devil',
level: 'high',
length: 1000,
example: 10
},
{
_id: ObjectId('60095b8a3fc110f90873ce2d'),
title: 'Graph',
author: 'Aditya',
level: 'high',
length: 1500,
example: 15
}
]
In the above Query, We have documented data which has the length attribute value greater than 510. So, we pass a filter query that is {length : {$gte : 510}} in the find() method.
What is a MongoDB Query?
MongoDB Query is a fundamental aspect of MongoDB that allows users to fetch data from the database. Similar to SQL queries in traditional databases, MongoDB queries provide simplicity and flexibility in retrieving specific data based on certain criteria or conditions.
In this article, We will learn about the MongoDB Query in detail by performing various Queries on document of MongoDB collection and so on.