Mongoose Schema.prototype.static() API
Last Updated :
19 Dec, 2022
The Mongoose Schema API.prototype.static() method of the Mongoose API is used on the Schema object. It allows us to define static class methods for the models using schema objects. We can access static method on schema object to define static methods. Let us understand static() method using an example.
Syntax:
schemaObject.static( method, callback );
Parameters: This method accepts two parameters as described below:
- method: It is used to specify the method name at the class level for the model.
- callback: It is used to specify the function which will do the specific task for the method.
Return Value: This method can return any value based on our function definition.
Setting up Node.js Mongoose Module:
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: The below example illustrates the functionality of the Mongoose Schema static() method. In this example, we have defined static method named findCustomerByAddress, we are fetching the document for which the value of address field value is Indore, and we are handling the returned value as a promise using then and catch block.
Filename: app.js
Javascript
const mongoose = require( "mongoose" );
const connectionObject = mongoose.createConnection(URI, {
useNewUrlParser: true ,
useUnifiedTopology: true ,
});
const customerSchema = new mongoose.Schema({
name: String,
address: String,
orderNumber: Number,
});
customerSchema.static( 'findCustomerByAddress' ,
function (address) {
return this .find({ address: address });
})
const Customer =
connectionObject.model( 'Customer' , customerSchema);
Customer.findCustomerByAddress( 'Indore' ).then(result => {
console.log(result);
}). catch (error => console.log(error));
|
Step 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("639ede899fdf57759087a655"),
name: 'Chintu',
address: 'Indore',
orderNumber: 6,
__v: 0
}
]
Example 2: The below example illustrates the functionality of the Mongoose Schema static() method. In this example, we have defined static method named as getAll, we are fetching all the documents from the database, and we are handling the returned value using anonymous asynchronous function.
Filename: app.js
Javascript
const mongoose = require( "mongoose" );
const connectionObject = mongoose.createConnection(URI, {
useNewUrlParser: true ,
useUnifiedTopology: true ,
});
const customerSchema = new mongoose.Schema({
name: String,
address: String,
orderNumber: Number,
});
customerSchema.static( 'getAll' , function () {
return this .find({});
})
const Customer =
connectionObject.model( 'Customer' , customerSchema);
(async () => {
const result = await Customer.getAll();
console.log(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:
[
{
_id: new ObjectId("639ede899fdf57759087a655"),
name: 'Chintu',
address: 'Indore',
orderNumber: 6,
__v: 0
},
{
_id: new ObjectId("639ede899fdf57759087a653"),
name: 'Aditya',
address: 'Mumbai',
orderNumber: 2,
__v: 0
},
{
_id: new ObjectId("639ede899fdf57759087a654"),
name: 'Bhavesh',
address: 'Delhi',
orderNumber: 5,
__v: 0
}
]
Reference: https://mongoosejs.com/docs/api/schema.html#schema_Schema-static
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...