In document middleware functions, the context of this refers to the document. A document has the following built-in methods and you can define hooks for them:
- init: This is called internally, immediately after a document is returned from MongoDB. Mongoose uses setters for marking the document as modified or which fields of the document were modified. init initializes the document without setters.
- validate: This executes built-in and custom set validation rules for the document.
- save: This saves the document in the database.
- remove: This removes the document from the database.