mongoose-populatedeprecated

Mini population hook for Mongoose so you don't need to keep rewriting this.populate and next.

Usage no npm install needed!

<script type="module">
  import mongoosePopulate from 'https://cdn.skypack.dev/mongoose-populate';
</script>

README

Mongoose Populate

npm bundle size npm GitHub GitHub top language GitHub Workflow Status (branch)

Mini population hook for Mongoose so you don't need to keep rewriting this.populate and next.

Installation

$ npm install mongoose-populate

CommonJS

const { Schema } = require('mongoose')
const { createPopulateHook } = require('mongoose-populate')

ESM

import { Schema } from 'mongoose'
import { createPopulateHook } from 'mongoose-populate'

Schema

const ExampleSchema = new Schema({
    example: {
        type: Schema.Types.ObjectId,
        ref: 'OtherModel',
    },
})

Hook replacement

- ExampleSchema.pre('find', function populateExampleHook(next) {
-   this.populate('example');
-   next();
- });

+ ExampleSchema.pre('find', createPopulateHook('example'));