In mongoose, Middlewares are also called as pre
and post
hooks.
Both of these middleware support pre and post hooks.
Document middleware
Its supported for document functions init
, validate
, save
and remove
Query middleware
Its supported for query functions count
, find
, findOne
, findOneAndRemove
, findOneAndUpdate
, insertMany
and update
.
There are two types of Pre hooks
serial
As the name suggests, Its executed in serial order i..e one after another
parallel
Parallel middleware offers more fine-grained flow control and the hooked method
is not executed until done
is called by all parallel middleware.
Post Middleware are executed after the hooked method
and all of its pre
middleware have been completed.
hooked methods are the functions supported by document middleware. init, validate, save, remove