Plugins are a tool for reusing logic in multiple schemas. Suppose you have several models in your database and want to add a property to each one. Just create a plugin once and apply it to each Schema
:
Want to register a plugin for all schemas? The mongoose singleton has a .plugin()
function that registers a plugin for every schema. For example:
The Mongoose team maintains several plugins that add cool new features to Mongoose. Here’s a couple:
- mongoose-autopopulate: Always certain fields in your Mongoose schemas.
- mongoose-lean-virtuals: Attach virtuals to the results of Mongoose queries when using .
Not only can you re-use schema functionality in your own projects, but you also reap the benefits of the Mongoose community as well. Any plugin published to npm and with ‘mongoose’ as an will show up on our search results page.