Adding Methods to the Schema Object

Mongoose schemas enable you to add to the Schema object methods that are automatically available on document objects in the model. This allows you to call the methods by using the Document object.

You add methods to the Schema object by assigning a function to the Schema.methods property. The function is just a standard JavaScript function assigned to the Document object. The Document object can be accessed by using the this keyword. For example, the following assigns a function named fullName to a model that returns a combination of the first and last names:

var schema = new Schema({
  first: String,
  last: String
});
schema.methods.fullName = function(){
  return this.first + " " + this.last;
};

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset