Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 10, 2025. It is now read-only.

Always populate() certain fields in your mongoose schemas

License

NotificationsYou must be signed in to change notification settings

mongodb-js/mongoose-autopopulate

Repository files navigation

Alwayspopulate() certain fields in your mongoose schemas

Build StatusCoverage Status

Read the docs here.

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.

Usage

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

Stars

Watchers

Forks

Packages

No packages published

Contributors11


[8]ページ先頭

©2009-2025 Movatter.jp