15

What is the difference between methods and statics?

Mongoose API defines statics as

Statics are pretty much the same as methods but allow for defining functions that exist directly on your Model.

What exactly does it mean? What does existing directly on models mean?

Statics code example from documentation:

AnimalSchema.statics.search = function search (name, cb) {
  return this.where('name', new RegExp(name, 'i')).exec(cb);
}

Animal.search('Rover', function (err) {
  if (err) ...
})
Joundill
  • 6,828
  • 12
  • 36
  • 50
raju
  • 4,788
  • 15
  • 64
  • 119
  • 1
    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. – WiredPrairie May 02 '14 at 10:45
  • But methods are also defined on models and work on all the instances of that model. Isn't it? – raju May 02 '14 at 11:28
  • Yes, they're both defined on models. It's just what they "act" upon that matters. – WiredPrairie May 02 '14 at 12:03
  • If models are tied to the instance, then why can I get access to the model in a static using 'this'? – G. Deward Aug 02 '16 at 18:11

2 Answers2

11

Think of a static like an "override" of an "existing" method. So pretty much directly from searchable documentation:

AnimalSchema.statics.search = function search (name, cb) {
   return this.where('name', new RegExp(name, 'i')).exec(cb);
}

Animal.search('Rover', function (err) {
  if (err) ...
})

And this basically puts a different signature on a "global" method, but is only applied when called for this particular model.

Hope that clears things up a bit more.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • In this case would AnimalSchema.search also have worked? If not, is it because search is already defined on all the models as a globel method? Is it similar to Java - Overriding where subclass redefined the method defined by super class. – raju May 02 '14 at 10:05
  • @raju That is pretty much the intention. Any other methods that would normally apply to a "model" can be called. As can you "model" custom methods. A "static" means "call this instead of.." so it is essentially an override where the `this` is the "super". – Neil Lunn May 02 '14 at 10:17
9

It seems like

'method' adds an instance method to documents constructed from Models

whereas

'static' adds static "class" methods to the Model itself

From the documentation:

Schema#method(method, [fn])

Adds an instance method to documents constructed from Models compiled from this schema.

var schema = kittySchema = new Schema(..);

schema.method('meow', function () {
  console.log('meeeeeoooooooooooow');
})

Schema#static(name, fn)

Adds static "class" methods to Models compiled from this schema.

var schema = new Schema(..);
schema.static('findByName', function (name, callback) {
  return this.find({ name: name }, callback);
});
Joundill
  • 6,828
  • 12
  • 36
  • 50
obai
  • 399
  • 5
  • 14