Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

mongoose
mongodb
communitycreator

What is gt() 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

gt() is a query to find documents that match a path where the value is greater than the value passed to the gt() method. In other words, gt() invokes the $gt query condition.

Syntax

query.gt()

Parameters

This method takes a value that is a number. gt() can also take an optional path parameter.

Return value

gt() returns documents with a path that is greater than the value passed to it.

Code

In the code below, we create a schema, make a query, and call gt() to return ages that are greater than 18.

// 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 `gt()` method
const adults = await Adult.find().where("age").gte(18)
// OR
const adults = await Adult.find().gt("age", 18)
/*
OUTPUT : adults is an arrary of objects
that has "age" greater than 18
*/

RELATED TAGS

mongoose
mongodb
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