Open In App

Mongoose Document Model.where() API

Last Updated : 31 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The Model.where() method of the Mongoose API is used to extract documents from a collection based on conditions. We can directly use where() on any model and on where() we can put various relational conditions to get the result. Model.where() returns array of objects. Each object consists of the document in a collection.

Setting up Node.js application:

Step 1: Create a Node.js application using the following command:

npm init

Step 2: After creating the NodeJS application, Install the required module using the following command:

npm install mongoose

Project Structure: The project structure will look like this:

 

Example 1: In this example, We have established a database connection using mongoose and defined model over customerSchema, having two columns “name”, and “orderCount”. In the end, we are using the where method on the Customer model which will give us an array of objects based on the condition we provide. In this example, we are finding documents where “orderCount” value is greater than 10.

  • app.js: Write down the below code in the app.js file:

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, orderCount: Number }
)
  
// Defining customerSchema model
const Customer = mongoose.model('Customer', customerSchema);
  
Customer.where('orderCount').gt(10).then(result => {
    console.log(result)
});


Steps to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

[
    {
        _id: new ObjectId("6304e7c8c21ca86f5ea6fce4"),
        name: 'Customer3',
        orderCount: 20,
        __v: 0
    }
]

Example 2: In this example, we are finding documents where the “orderCount” value is greater than 5 and less than 20.

  • app.js: Write down the below code in the app.js file:

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, orderCount: Number }
)
  
// Defining customerSchema model
const Customer = mongoose.model('Customer', customerSchema);
  
Customer.where('orderCount').gt(5).lt(20).then(result => {
    console.log(result)
});


Steps to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

[
    {
        _id: new ObjectId("6304e7c8c21ca86f5ea6fce3"),
        name: 'Customer2',
        orderCount: 10,
        __v: 0
    },
    {
        _id: new ObjectId("6305fa9f63f7ccbdbc0ee95c"),
        name: 'Customer3',
        orderCount: 10,
        __v: 0
    },
    {
        _id: new ObjectId("6305fa9f63f7ccbdbc0ee95d"),
        name: 'Customer4',
        orderCount: 10,
        __v: 0
    }
]

Reference: https://mongoosejs.com/docs/api/model.html#model_Model-where



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads