What is gte() in Mongoose?

Overview

In Mongoose, there are many query functions that we use when using Mongoose on our MongoDB database.

Some of the functions include:

  • lte()
  • map()
  • lt()
  • gt()
  • ne()

The gte() method

In this shot, we will look at the gte() or Query.prototype.gte() method.

gte() is used to specify a $gte() query condition and denotes “greater than or equal to.”

The gte() method returns documents that match the query condition where the path is greater than or equal to the condition.

We use the where() method with gte() when there is only one path.

Syntax


query.gte(val)
// OR
Query.prototype.gte(val)

Parameters

gte() takes a value that is a Number and can take an optional path parameter that is a String.

Code

We will demonstrate the use of gte() by creating a Schema, making a query, and then matching the query with the Schema.

// import mongoose
const mongoose = require("mongoose");
// creating a Schema with mongoose
let Adult = mongoose.model("adult", new mongoose.Schema({
name: String,
age: Number
})
)
// make a query and use the `gte()` method
const adults = await Adult.find().where("age").gte(18)
// OR
const adults = await Adult.find().gte("age", 18)
/*
OUTPUT : adults is an arrary of objects
that has "age" greater than or equal to 18
*/

Free Resources