Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

mongoose
communitycreator

What is gte() in Mongoose?

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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
*/

RELATED TAGS

mongoose
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring