Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment.
Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.
Mongoose makes it painlessly easy to work with MongoDB database.
We can easily structure our database using Schemas and Models
, Automate certain things when record is added or updated using Middlewares/Hooks
and easily get the data we need by querying
our models.
Important Links
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