Schema.prototype.static()

Parameters
  • name «String|Object»
  • [fn] «Function»

Adds static “class” methods to Models compiled from this schema.

Example

  1. const schema = new Schema(..);
  2. // Equivalent to `schema.statics.findByName = function(name) {}`;
  3. schema.static('findByName', function(name) {
  4. return this.find({ name: name });
  5. });
  6. const Drink = mongoose.model('Drink', schema);
  7. await Drink.findByName('LaCroix');

If a hash of name/fn pairs is passed as the only argument, each name/fn pair will be added as statics.