Introduction to MongoDB $gte Operator

The $gte operator in MongoDB is used to compare whether the value of a field is greater than or equal to a specified constant value.

Syntax

The syntax of the $gte operator in MongoDB is as follows:

{
  field: {
    $gte: value
  }
}

Here, field represents the name of the field to be compared, and value represents the constant value to be compared.

Use cases

The $gte operator in MongoDB is commonly used to query documents where the value of a field is greater than or equal to a specified value. For example, it can be used to query student records with scores greater than or equal to 80 points, or to query order records with order amounts greater than or equal to 1000 yuan.

Examples

Example 1: Querying student records with scores greater than or equal to 80

Suppose we have a collection students that contains information about many students, including their student numbers, names, and scores. Now, we need to query student records with scores greater than or equal to 80, which can be done using the $gte operator:

db.students.find({ score: { $gte: 80 } })

Assuming the data in the collection is as follows:

{ "_id": 1, "name": "Tom", "score": 76 }
{ "_id": 2, "name": "Jack", "score": 82 }
{ "_id": 3, "name": "Lucy", "score": 90 }
{ "_id": 4, "name": "Alice", "score": 65 }

The above query will return the following results:

{ "_id": 2, "name": "Jack", "score": 82 }
{ "_id": 3, "name": "Lucy", "score": 90 }

Example 2: Querying order records with order amounts greater than or equal to 1000 yuan

Suppose we have an orders collection that contains the following order information:

{
  orderId: 1,
  orderDate: ISODate("2022-02-28T10:00:00Z"),
  orderStatus: "Shipped",
  orderAmount: 800.0
},
{
  orderId: 2,
  orderDate: ISODate("2022-03-01T09:30:00Z"),
  orderStatus: "Paid",
  orderAmount: 1200.0
},
{
  orderId: 3,
  orderDate: ISODate("2022-03-02T11:00:00Z"),
  orderStatus: "Canceled",
  orderAmount: 500.0
},
{
  orderId: 4,
  orderDate: ISODate("2022-03-03T14:00:00Z"),
  orderStatus: "Shipped",
  orderAmount: 1500.0
}

Now, we need to query order records with order amounts greater than or equal to 1000 yuan, which can be done using the following code:

db.orders.find({ orderAmount: { $gte: 1000 } })

After executing the above code, the order records with orderId 2 and 4 will be returned:

{ "orderId": 2, "orderDate": ISODate("2022-03-01T09:30:00Z"), "orderStatus": "Paid", "orderAmount": 1200.0 }
{ "orderId": 4, "orderDate": ISODate("2022-03-03T14:00:00Z"), "orderStatus": "Shipped", "orderAmount": 1500.0 }

By using the $gte operator, we can easily query for order records with an order amount greater than or equal to 1000.

Conclusion

In MongoDB, the $gte operator can be used to query documents with field values greater than or equal to a given value. It can be used in various scenarios, such as querying for students with grades greater than or equal to a certain value or querying for orders with an order amount greater than or equal to a certain value. When using the $gte operator, it is important to note that the value in the query condition should be of the same type as the field, otherwise there may be query errors.