Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of mongoose methods and statics?

What is the use of mongoose methods and statics and how are they different from normal functions?

Can anyone explain the difference with example.

like image 628
iiR Avatar asked Sep 26 '16 17:09

iiR


People also ask

What is static method in Mongoose?

mongoose Mongoose Schemas Schema Statics Schema Statics are methods that can be invoked directly by a Model (unlike Schema Methods, which need to be invoked by an instance of a Mongoose document). You assign a Static to a schema by adding the function to the schema's statics object.

What is the difference between statics and methods in Mongoose?

Methods operate on an instance of a model. Statics behave as helper functions only and can perform any action you want, including collection level searching. They aren't tied to an instance of a Model. But methods are also defined on models and work on all the instances of that model.

What is method in Mongoose?

Mongoose provides 2 ways of doing this, methods and statics. Methods adds an instance method to documents whereas Statics adds static "class" methods to the Models itself. Given the example Animal Model below: var AnimalSchema = mongoose. Schema({ name: String, type: String, hasTail: Boolean }); module.

Can you define methods in Mongoose schema?

Each Schema can define instance and static methods for its model.


2 Answers

Database logic should be encapsulated within the data model. Mongoose provides 2 ways of doing this, methods and statics. Methods adds an instance method to documents whereas Statics adds static "class" methods to the Models itself.

Given the example Animal Model below:

var AnimalSchema = mongoose.Schema({    name: String,    type: String,    hasTail: Boolean  });    module.exports = mongoose.model('Animal', AnimalSchema);

We could add a method to find similar types of animal, and a static method to find all animals with tails:

AnimalSchema.methods.findByType = function (cb) {    return this.model('Animal').find({ type: this.type }, cb);  };    AnimalSchema.statics.findAnimalsWithATail = function (cb) {    Animal.find({ hasTail: true }, cb);  };

Here's the full model with example usage for methods and statics:

var AnimalSchema = mongoose.Schema({    name: String,    type: String,    hasTail: Boolean  });    AnimalSchema.methods.findByType = function (cb) {    return this.model('Animal').find({ type: this.type }, cb);  };    AnimalSchema.statics.findAnimalsWithATail = function (cb) {    Animal.find({ hasTail: true }, cb);  };    module.exports = mongoose.model('Animal', AnimalSchema);    // example usage:    var dog = new Animal({    name: 'Snoopy',    type: 'dog',    hasTail: true  });    dog.findByType(function (err, dogs) {    console.log(dogs);  });    Animal.findAnimalsWithATail(function (animals) {    console.log(animals);  });
like image 130
Sha Alibhai Avatar answered Oct 11 '22 12:10

Sha Alibhai


If i wanted to retrieve animals with hasTail i could simply change this line of code:

return this.model('Animal').find({ type: this.type }, cb); 

to:

return this.model('Animal').find({ hasTail: true }, cb); 

and i would not have to create a statics function.

Use method on individual documents if you want to manipulate the individual document like adding tokens etc. Use the statics approach if you want query the whole collection.

like image 29
soulsako20 Avatar answered Oct 11 '22 12:10

soulsako20