Schema.prototype.post()

Parameters
  • The «String|RegExp» method name or regular expression to match method name

  • [options] «Object»

  • [options.document] «Boolean» If name is a hook for both document and query middleware, set to true to run on document middleware.

  • [options.query] «Boolean» If name is a hook for both document and query middleware, set to true to run on query middleware.

  • fn «Function» callback

Defines a post hook for the document

  1. const schema = new Schema(..);
  2. schema.post('save', function (doc) {
  3. console.log('this fired after a document was saved');
  4. });
  5. schema.post('find', function(docs) {
  6. console.log('this fired after you ran a find query');
  7. });
  8. schema.post(/Many$/, function(res) {
  9. console.log('this fired after you ran `updateMany()` or `deleteMany()`);
  10. });
  11. const Model = mongoose.model('Model', schema);
  12. const m = new Model(..);
  13. m.save(function(err) {
  14. console.log('this fires after the `post` hook');
  15. });
  16. m.find(function(err, docs) {
  17. console.log('this fires after the post find hook');
  18. });