- Notifications
You must be signed in to change notification settings - Fork39
Always populate() certain fields in your mongoose schemas
License
mongodb-js/mongoose-autopopulate
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Alwayspopulate() certain fields in your mongoose schemas
Note: population is a powerful feature, but it has limitations andhelps you get away with poor schema design. In particular, it is usuallybad MongoDB schema design to include arrays that grow without bound inyour documents. Do not include a constantly-growing array of ObjectIdsin your schema - your data will become unwieldy as the array grows andyou will eventually hit the16 MB document size limit.In general, think carefully when designing your schemas.
Themongoose-autopopulate module exposes a single function that you canpass toMongoose schema'splugin() function.
constschema=newmongoose.Schema({populatedField:{type:mongoose.Schema.Types.ObjectId,ref:'ForeignModel',// The below option tells this plugin to always call `populate()` on// `populatedField`autopopulate:true}});schema.plugin(require('mongoose-autopopulate'));
Only apply this plugin to top-level schemas. Don't apply this plugin to child schemas.
// Don't do `nestedSchema.plugin(require('mongoose-autopopulate'))`.// You only need to add mongoose-autopopulate to top-level schemas.constnestedSchema=mongoose.Schema({child:{type:Number,ref:'Child',autopopulate:true}});consttopSchema=mongoose.Schema({nested:nestedSchema});topSchema.plugin(require('mongoose-autopopulate'));
About
Always populate() certain fields in your mongoose schemas
Topics
Resources
License
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors11
Uh oh!
There was an error while loading.Please reload this page.