1.1.0 • Published 6 months ago

mongoose-autopopulate v1.1.0

Weekly downloads
12,446
License
Apache 2.0
Repository
github
Last release
6 months ago

mongoose-autopopulate

Always populate() certain fields in your mongoose schemas

Build Status Coverage Status

Read the docs here.

Note: population is a powerful feature, but it has limitations and helps you get away with poor schema design. In particular, it is usually bad MongoDB schema design to include arrays that grow without bound in your documents. Do not include a constantly-growing array of ObjectIds in your schema - your data will become unwieldy as the array grows and you will eventually hit the 16 MB document size limit. In general, think carefully when designing your schemas.

Usage

The mongoose-autopopulate module exposes a single function that you can pass to Mongoose schema's plugin() function.

const schema = new mongoose.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.
const nestedSchema = mongoose.Schema({
  child: { type: Number, ref: 'Child', autopopulate: true }
});
const topSchema = mongoose.Schema({ nested: nestedSchema });
topSchema.plugin(require('mongoose-autopopulate'));
1.1.0

6 months ago

1.0.1

12 months ago

1.0.0

1 year ago

1.0.0-rc0

1 year ago

0.17.0

1 year ago

0.17.1

1 year ago

0.16.1

2 years ago

0.16.0

3 years ago

0.15.0

3 years ago

0.14.0

3 years ago

0.13.0

3 years ago

0.12.3

3 years ago

0.12.2

4 years ago

0.12.1

4 years ago

0.12.0

4 years ago

0.11.0

4 years ago

0.10.0

4 years ago

0.9.1

5 years ago

0.9.0

5 years ago

0.8.2

6 years ago

0.8.1

6 years ago

0.8.0

6 years ago

0.7.0

6 years ago

0.6.1

6 years ago

0.6.0

7 years ago

0.5.0

7 years ago

0.4.0

9 years ago

0.3.0

9 years ago

0.2.0

9 years ago

0.1.0

9 years ago