EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB $gte
 

MongoDB $gte

Lalita Gupta
Article byLalita Gupta
EDUCBA
Reviewed byRavi Rathore

Updated December 13, 2023

What is MongoDB $gte Operator?

MongoDB has various operators for comparison. The $gte is a MongoDB operator where you can select values greater than or equal (>=) to a given value in documents. You can use $gte in methods like find(), update(), etc.

 

 

Various comparison operators can perform comparisons on fields where the BSON type matches the value of the query. MongoDB provides only limited support for cross-BSON comparison through the backing type.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Table of contents

  • What is MongoDB $gte operator?
  • Compatibility
  • Syntax
  • Update based on Embedded Document Fields
  • Examples using the $gte operator in MongoDB
    • Query-1
    • Query-2
    • Query-3
    • Query-4

Compatibility

$gte is used for deployment hosted in these MongoDB environments:

  • Atlas: Cloud-managed MongoDB
  • Enterprise: Paid, self-managed MongoDB
  • Community: Free, self-managed MongoDB

Syntax

The syntax of this $gte operator is as follows:

{ field: { $gte: value } }

Consider this example, where we will use the $lt operator.

We will create an “educba” db in Mongodb in the shell:

Create educba db

Now, we will create an “inventory” collection under the “educba” db:

create inventory collection

These will be inserted successfully.

Now, we will insert these values in the “inventory” collection:

db.inventory.insertMany( [
   {
      "item": "screws", "quantity": 20,
      "carrier": { "name": "Shipit", "fee": 2 }
   },
   {
      "item": "nails", "quantity": 40,
      "carrier": { "name": "Shipit", "fee": 5 }
   },
   {
      "item": "rivets", "quantity": 15,
      "carrier": { "name": "Shipit", "fee": 2.5 }
   },
   {
      "item": "nuts", "quantity": 30,
      "carrier": { "name": "Shipit", "fee": 3 }
   },
   {
      "item": "bolts", "quantity": 50,
      "carrier": { "name": "Shipit", "fee": 4 }
   },
   {
      "item": "washers", "quantity": 10,
      "carrier": { "name": "Shipit", "fee": 1 }
   }
] )

Insert values in inventory collection

These will be inserted successfully.

We have added this to collections of MongoDB databases. Now, we will query the following:

db.inventory.find({ "quantity": { "$gte": 30 } })

This query finds all values with a quantity greater than or equal to 30. There will be three outputs:

Greater than or equal to 30

Update based on Embedded Document Fields

You can update based on embedded document fields using the $gte operator.

For example, if you want to increase the fee for carriers whose quantity is greater than or equal to 4. Then you can use the following command:

db.inventory.updateMany(
   { "carrier.fee": { "$gte": 4 } },
   { "$inc": { "carrier.fee": 2 } }
)

This query uses the $gte operator to find documents where the fee inside the carrier-embedded document is greater than or equal to 4. Then, the $inc operator increases the cost by 2.

The final documents are after the above update query:

update query

Examples Using $gte Operator in MongoDB

Consider another collection to understand the $gte operator in the given examples.

Using the MongoDB shell, we will create “devices” db:

Create Device db

We will create a collection of “devices” under the “devices” db:

Collection of Device

Now, we will insert these values into the “devices” collection:

db.devices.insertMany([
    { "_id": 1, "deviceName": "UltraBook", "cost": 999, "manufactureDate": ISODate("2018-03-22"), "specs": { "ram": 8, "display": 13.3, "processor": 2.5 }, "colors": ["silver", "black"], "storage": [128, 256, 512] },
    { "_id": 2, "deviceName": "TabMax", "cost": 1199, "manufactureDate": ISODate("2019-07-10"), "specs": { "ram": 12, "display": 10.5, "processor": 3.2 }, "colors": ["white", "black", "blue"], "storage": [256, 512, 1024] },
    { "_id": 3, "deviceName": "SmartPad Pro", "cost": 899, "manufactureDate": ISODate("2020-12-05"), "specs": { "ram": 6, "display": 9.7, "processor": 2.8 }, "colors": ["gray", "rose gold"], "storage": [64, 128, 256] },
    { "_id": 4, "deviceName": "SmartDesk", "cost": 1499, "manufactureDate": ISODate("2022-05-18"), "specs": { "ram": 16, "display": 27, "processor": 4.0 }, "colors": ["black", "white"], "storage": [512, 1024, 2048] },
    { "_id": 5, "deviceName": "SmartPhone Pro", "cost": 1299, "manufactureDate": ISODate("2023-10-08"), "specs": { "ram": 8, "display": 6.2, "processor": 3.0 }, "colors": ["gold", "blue", "green"], "storage": [256, 512] }
]);

These will be inserted successfully:

Values inserted

This collection has fields: deviceName, cost, manufactureDate, specs, colors, and storage. These documents are used in examples with the $gte operator for the following various queries.

Query 1

Select documents where the cost is greater than or equal to 1199:

db.devices.find({
    cost: {
        $gte: 1199
    }
}, {
    deviceName: 1,
    cost: 1
})

The output will be,

MongoDB $gte - output 1

Query 2

Check if the display size in the specs document is greater than or equal to 10:

db.devices.find({
    "specs.display": {
        $gte: 10
    }
}, {
    deviceName: 1,
    "specs.display": 1
})

The output will be,

MongoDB $gte - output 2

Query 3

Check if an array element in storage is greater than or equal to 1024:


db.devices.find({
    storage: {
        $gte: 1024
    }
}, {
    deviceName: 1,
    storage: 1
})

The output will be,

MongoDB $gte - output 3

Query 4

Select documents where the manufacture date is after or on 2022-01-01:

db.devices.find({
    manufactureDate: {
        $gte: new ISODate('2022-01-01')
    }
}, {
    deviceName: 1,
    manufactureDate: 1
});

The output will be,

MongoDB $gte - output 4

Conclusion

The $gte is an operator in MongoDB that selects documents with values greater than or equal (>=) to the given value. You can find and update values by using the $gte operator. MongoDB’s $gte operator efficiently selects and updates documents based on specified criteria.

FAQ’s

Q1. How is the $gte operator similar to other MongoDB comparison operators?
Answer: The $gte in MongoDB is used for comparison. Other comparison operators in the MongoDB include $gt (greater than) and $eq (equal). But note that the $gte (greater than or equal to) operator consists of both this $ gt (greater than) and $eq (equal). This property provides flexibility for various queries and filtering based on multiple values.

Q2. In which MongoDB environments can the $gte operator be deployed?
Answer: The $gte operator can be used in MongoDB environments like Altas (cloud-managed MongoDB), Enterprise(paid and self-managed MongoDB), and community (free and self-managed MongoDB).

Q3. How do you use the $gte operator to update documents based on embedded document fields?
Answers: You can update documents based on embedded document fields using the $gte operator. You can use the updateMany() method. For example, if you want to increase fees for carriers whose quantity is greater than or equal to 30. You can use this command:

db.inventory.updateMany(
   { "carrier.fee": { "$gte": 30 } },
   { "$inc": { "carrier.fee": 2 } }
)

This will find documents where the fee inside the carrier-embedded documents is greater than or equal to 30 and then increment the fee by 2.

Q4. Can you use the $gte operator for date-based queries in MongoDB?
Answer: We can use the $gte operator for date-based operator queries. For example, if you want to select documents where the manufacture date is after or on January 1, 2022. You can use this command:

db.devices.find({
    manufactureDate: {
        $gte: new ISODate('2022-01-01')
    }
}, {
    deviceName: 1,
    manufactureDate: 1
});

Recommended Articles

We hope this EDUCBA information on “MongoDB $gte” benefited you. You can view EDUCBA’s recommended articles for more information.

  1. MongoDB $lt
  2. Examples of MongoDB $eq
  3. MongoDB $elemMatch
  4. MongoDB Query Operators

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW