Schema.prototype.method()

Parameters
  • method «String|Object» name

  • [fn] «Function»

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

Example

  1. const schema = kittySchema = new Schema(..);
  2. schema.method('meow', function () {
  3. console.log('meeeeeoooooooooooow');
  4. })
  5. const Kitty = mongoose.model('Kitty', schema);
  6. const fizz = new Kitty;
  7. fizz.meow(); // meeeeeooooooooooooow

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

  1. schema.method({
  2. purr: function () {}
  3. , scratch: function () {}
  4. });
  5. // later
  6. fizz.purr();
  7. fizz.scratch();

NOTE: Schema.method() adds instance methods to the Schema.methods object. You can also add instance methods directly to the Schema.methods object as seen in the guide