mongoose-populator

Allows full population of nested models in mongoose

Usage no npm install needed!

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

README

mongoose-populator Build Status

Recursive population of mongoose documents, including lean option

Usage

Requiring the module will return the population function

var populate = require('mongoose-populator');

The signature for this function: populate(docs, paths, callback, [lean])

For example, populating some cars, as in the test folder:

Car.findOne({ model: 'Focus' }, function(err, cars){
    populate(cars, 'maker.owner', function(err, populatedCars){
        // Use the result
    });
});

This will populate the maker field on each car, as well as the owner field on each maker

Multiple fields can be requested, separated by spaces, of any arbitrary length and can also include arrays at any depth: 'some.field some.other.field'

Note each field along the chain will be populated, including array fields.

You can also specify the lean option, which will cause all returned documents, nested or otherwise, to be leaned out.