Open In App

Mongoose Query.prototype.gte() API

Improve
Improve
Like Article
Like
Save
Share
Report

Mongoose is an Object Data Modeling (ODM) library for MongoDB. It defines a strongly-typed schema, with default values and schema validations which are later mapped to a MongoDB document. 

This method is used to select those documents where the value of the field is greater than or equal to(>=) the given value.

Syntax:

Modal.find().where([path]).gte(val)

Parameters:

  • path: It is a string that represents the field name, this is a must-have parameter.
  • val: It is the number, greater than which we want to find the documents of that path.

Installation of mongoose module:

Step 1: You can visit the link to Install the mongoose module. You can install this package by using this command.

npm install mongoose

Step 2: After installing the mongoose module, you can check your mongoose version in the command prompt using the command.

npm version mongoose

Step 3: After that, you can just create a folder and add a file for example index.js, To run this file you need to run the following command.

node index.js

Project Structure: The project structure will look like this:

 

Example 1: We have some data of customers that contains their names, interests, and orderCount. In this example, we want to find customers whose order counts are greater than or equal to 6.

Javascript




// Require mongoose module
const mongoose = require('mongoose');
 
// Set Up the Database connection
mongoose.connect(
    useNewUrlParser: true,
    useUnifiedTopology: true
})
 
 
// Defining customerSchema schema
const customerSchema = new mongoose.Schema({
    name: String,
    interest: Array,
    orderCount: Number
})
 
// Defining customerSchema model
const Customer = mongoose.model(
    'Customer', customerSchema);
 
// Find the number of customers whose
// orderCount are greater than 6
Customer.find().where("orderCount")
.gte(6).then((res) => {
    console.log(res)
});


Steps to run the application:

Step 1: Below is the sample data in the database before the function is executed, You can use any GUI tool or terminal to see the database like we have used the MongoDB compass GUI tool as shown below:

 

Step 2: Run the index.js file using the below command:

node index.js

 

Example 2: In this example, we are passing nothing as a value in this method. We see that it returns all the documents of that field.

Javascript




// Require mongoose module
const mongoose = require('mongoose');
 
// Set Up the Database connection
mongoose.connect(
    useNewUrlParser: true,
    useUnifiedTopology: true
})
 
// Defining customerSchema schema
const customerSchema = new mongoose.Schema({
    name: String,
    interest: Array,
    orderCount: Number
})
 
// Defining customerSchema model
const Customer = mongoose.model(
    'Customer', customerSchema);
 
Customer.find().where("orderCount")
.gte().then((res) => {
    console.log(res)
});


Step to run the application: Run the index.js file using the below command:

node index.js

 

Reference: https://mongoosejs.com/docs/api/query.html#query_Query-gte



Last Updated : 29 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads