Updated December 20, 2023
MongoDB $gt operator
The MongoDB $gt operator is one of the comparison operators used for querying data in MongoDB. It stands for “greater than” (>) and is used to filter documents where a specified field’s value is greater than a given value. You can use $gt in methods like find(), update(), etc.
There are several comparison operators that can perform comparisons on fields where the BSON type matches the value of the query. MongoDB provides only limited support for cross-BSON comparison via backing types.
Table of Content
- MongoDB $gt operator
- Syntax
- Update based on Embedded Document Fields
- Examples Using $gt operator in MongoDB
Syntax
The syntax of this $gt operator is as follows:
{field: { $gt: value } }
- field: This is the name of the field you want to compare.
- $gt: This is the operator itself, indicating “greater than”.
- value: This is the value you want to compare the field against.
Consider this example, where we will use the $gt operator.
We will create an “educba” db in MongoDB in the shell:
Now, we will create an “inventory” collection under the “educba” db:
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 }
}
] )
These will be inserted successfully.
We have added this to collections of MongoDB databases. Now, we will query the following:
db.inventory.find({ "quantity": { "$gt": 30 } })
This query finds all values which have a quantity greater than 30. There will be two outputs:
Update Based on Embedded Document Fields
You can update based on embedded document fields using the $gt operator.
For example, if you want to increase fees for carriers whose volume exceeds 4. So you can use the following command:
db.inventory.updateMany(
{"carrier.fee": { "$gt": 4 } },
{ "$inc": { "carrier.fee": 2 } }
)
This query uses the $gt operator to find documents where the charge inside the carrier-embedded document exceeds 4. First, we filter {“carrier.fee”: { “$gt”: 4 } }, then it will update all those values. Then, the $inc operator increases the charge to 2. The final documents are after the above update query:
Examples Using $gt Operator in MongoDB
Consider another collection to understand the $gt operator in the given examples.
Using the MongoDB shell, we will create “devices” db:
We will create a collection of “devices” under the “devices” db:
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] }
]);
It will be inserted successfully:
This collection has fields: deviceName, cost, manufacture Date, specs, colors, and storage. These documents are used in examples with the $gt operator for the following various queries.
Example 1
Select documents where the cost is greater than 1199:
db.devices.find({
cost: {
$gt: 1199
}
}, {
deviceName: 1,
cost: 1
})
The output will be,
Example 2
Check if the display size in the specs document is greater than 10:
db.devices.find({
"specs.display": {
$gt: 10
}
}, {
deviceName: 1,
"specs.display": 1
})
The output will be,
Example 3
Check if an array element in storage is greater than 1024:
db.devices.find({
storage: {
$gt: 1024
}
}, {
deviceName: 1,
storage: 1
})
The output will be,
Example 4
Select documents where the manufacture date is after 2022-01-01:
db.devices.find({
manufactureDate: {
$gt: new ISODate('2022-01-01')
}
}, {
deviceName: 1,
manufactureDate: 1
});
The output will be,
Conclusion
The $gt is an operator in MongoDB that selects documents with a value greater than (>) the given value. You can find and update values using the $gt operator. MongoDB’s $gt operator efficiently selects and updates documents based on specified criteria.
FAQ’s
Q1. How does the $gt operator work with the find() method?
Answer: In the find() method, the $gt operator filters documents where the specified field exceeds a given value. For example, db.collection.find({ field: { $gt: value } }) retrieves documents where the field value is greater than the given value.
Q2. Can I use the $gt operator to update documents based on embedded document fields?
Answer: Yes. You can update documents based on embedded document fields using the $gt operator. For example, you can increase fees for carriers whose volume exceeds a given value. It uses the $gt operator in combination with the updateMany() method.
Q3. Can I combine the $gt operator with other operators in a single query?
Answer: Yes. You can combine the $gt operator with other operators in a query to create more complex conditions. It provides you the flexibility to construct queries for your specific requirements.
Q4. Can the $gt operator be used on non-numeric fields?
Answer: Yes. This $gt operator can be used for non-numeric fields as well. This operator is used for numerical comparisons. It can also be used with date fields, string fields, and other BSON types. You can filter documents based on various data types.
Recommended Articles
We hope this EDUCBA information on “MongoDB $gt” benefited you. You can view EDUCBA’s recommended articles for more information.