What is lte() in Mongoose?

Overview

lte() is a Mongoose function that is used to specify the $lte query condition.

lte means less than or equal to. In the same vein, lte() returns documents that are less than or equal to the specified condition.

Syntax


query.lte()
// OR
Query.prototype.lte()

Parameter

It can take an optional path parameter, and takes a value that is a type of Number.

Code

In the example below, we will create a Schema, make a query, and use the lte() function.

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

Explanation

From the code above, persons will be an array of Mongoose documents that have a value in the age field less than or equal to 70.

Free Resources