Open In App

Mongoose Schemas Query Helpers

Improve
Improve
Like Article
Like
Save
Share
Report

Mongoose is a MongoDB object modeling and handling for a node.js environment. Mongoose Schema Query Helpers are like instance methods for Mongoose queries. These query helpers can be used to filter out mongoose query results or perform additional operations on the existing result.

Creating node application And Installing Mongoose:

Step 1: Create a node application using the following command:

mkdir folder_name
cd folder_name
npm init -y

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

npm install mongoose

Project Structure: It will look like the following.

 

Example 1: In this example, we will create a query helper that will allow us to filter out the documents by the animal type that we will provide as an argument.

Filename: main.js

Javascript




const mongoose = require('mongoose')
  
// Database connection
    dbName: 'event_db',
    useNewUrlParser: true,
    useUnifiedTopology: true
}, err => err ? console.log(err) : 
    console.log('Connected to database'));
  
const animalSchema = new mongoose.Schema({ 
    name: String, type: String }, {
  
    // Assign a function to the "query" object of our 
    // animalSchema through schema options.
    // By following this approach, there is no need 
    // to create a separate TS type to define the 
    // type of the query functions. 
    query: {
        byType(type) {
            return this.where({ type })
        }
    }
}
);
  
const Animal = mongoose.model("Animal", animalSchema);
  
const animals = [
    {
        name: 'bond',
        type: 'dog'
    },
    {
        name: 'cevin',
        type: 'cat'
    }
]
  
Animal.insertMany(animals, (err, res) => {
    Animal.find().byName('cat').exec((err, animals) => {
        console.log(animals);
    });
})


Step to Run Application: Run the application using the following command from the root directory of the project:

node main.js

Output: 

 

Example 2:  In this example, we will create a query helper that will allow us to filter out the documents by the animal name that we will provide as an argument.

Filename: main.js

Javascript




const mongoose = require('mongoose')
  
// Database connection
    dbName: 'event_db',
    useNewUrlParser: true,
    useUnifiedTopology: true
}, err => err ? console.log(err) : console.log
    ('Connected to database'));
  
const animalSchema = new mongoose.Schema({ 
    name: String, type: String }, {
  
    // Assign a function to the "query" object of our 
    // animalSchema through schema options.
    // By following this approach, there is no need 
    // to create a separate TS type to define the 
    // type of the query functions. 
    query: {
        byName(name) {
            return this.where({ name })
        }
    }
}
);
  
const Animal = mongoose.model("Animal", animalSchema);
  
const animals = [
    {
        name: 'bond',
        type: 'dog'
    },
    {
        name: 'cevin',
        type: 'cat'
    }
]
  
Animal.insertMany(animals, (err, res) => {
    Animal.find().byName('bond').exec((err, animals) => {
        console.log(animals);
    });
})


Step to Run Application: Run the application using the following command from the root directory of the project:

node main.js

Output:

 

Reference: https://mongoosejs.com/docs/guide.html#query-helpers



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