Open In App

Mongoose Query.prototype.mod() API

Last Updated : 16 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The Mongoose Query API.prototype.mod() method of the Mongoose API is used on the Query objects. It allows us to provide condition in the form of modulus. Using this method we can select the documents where the value of the field divided by a divisor has the specified remainder. Let us understand mod() method using an example.

Syntax:

query.mod( path, [dividend, divisor] );

Parameters: This method accepts two parameters as discussed below:

  • path: It is used to specify the update query expression in the form of object.
  • array: It is used to specify the divisor and reminder values.

Return Value: This method returns query object on which we can call callback function.

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:

 

Database Structure: The database structure will look like this, the following documents are present in the collection.

 

Example 1: In this example, we are illustrating the functionality of mod() method. At the end, we are getting 15 value as reminder which matches to one of the document in collection.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
 
// Set Up the Database connection
 
const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
 
const Customer = connectionObject.model(
    "Customer",
    new mongoose.Schema({
        name: String,
        address: String,
        orderNumber: Number,
    })
);
 
const query = Customer.find();
query.mod("orderNumber", [100, 15])
query.exec((error, result) => {
    if (error) {
        console.log("Error -", error);
    } else {
        console.log("Result -", result);
    }
})


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

node app.js

Output:

Result - [
  {
    _id: new ObjectId("639ede899fdf57759087a655"),
    name: 'Chintu',
    address: 'Indore',
    orderNumber: 15,
    __v: 0
  }
]

Example 2: In this example, we are illustrating the functionality of mod() method.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
 
// Set Up the Database connection
 
const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
 
const Customer = connectionObject.model(
    "Customer",
    new mongoose.Schema({
        name: String,
        address: String,
        orderNumber: Number,
    })
);
 
const query = Customer.find();
query.mod("orderNumber", [25, 5])
query.then((res => {
    console.log(res);
})).catch((err) => {
    console.log(err);
})


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

node app.js

Output:

[]

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



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

Similar Reads