MongoDB – Greater than equals to Operator $gte

MongoDB provides different types of comparison operators and greater than equals to operator($gte) is one of them. This operator is used to select those documents where the value of the field is greater than equals to(>=) the given value. You can use this operator in methods (like, find(), update(), etc.) according to your requirements.

Syntax:

{field: {$gte: value}}

In the following examples, we are working with:

Database: GeeksforGeeks
Collection: employee
Document: four documents that contain the details of the employees in the form of field-value pairs.



 
Example #1:
In this example, we are selecting those documents where the value of the salary field is greater than equals to 35000.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.employee.find({salary: {$gte: 35000}}).pretty()

chevron_right


 
Example #2:
In this example, we are selecting only those documents where the age of the employee is greater than equals to 24. Or in other words, in this example, we are specifying conditions on the field in the embedded document using dot notation.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.employee.find({"personalDetails.age": {$gte: 24}}).pretty()

chevron_right


 
Example #3:

In this example, we are selecting only those documents where the points array is greater than equals to the specified array.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.employee.find({points: {$gte: [6, 7]}}).pretty()

chevron_right


 
Example #4:

In this example, we are updating the salary of those employees whose experience year is greater than equals to 2 years. Or in other words, set the value of the salary field to 60000 of those documents whose experienceYear field value is greater than equals to 2.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.employee.update({experienceYear: {$gte: 2}}, {$set: {salary: 60000}})

chevron_right


Note: The update() method by default update only one document at a time. If you want to update multiple documents, then set the value of its multi parameter to true. So, in this example, the update() method updated the first document that matches the given condition as shown in the below image.

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.