A Mongoose plugin for applying query options to every layer of population.
npm install mongoose-populate-options --save
const setPopulateOptions = require('mongoose-populate-options');
MySchema.plugin(setPopulateOptions);
MyModel.find()
.populate('friends')
.populate('teachers')
.setPopulateOptions({ limit: 2 });
This will set mognoose options on the two populate calls, as if you'd defined options inline on each one.
Note: Just like setOptions
, order matters. setPopulateOptions
will affect all populate calls before it, but not after it. Options from setPopulateOptions
will overwrite options that have alread been set.
Note: This method does not set options on the query itself.
You can restrict which options are allowed to be set in the options of populate commands. This can be useful when handling API requests where any arbitrary option can be sent by the client.
MyModel.find()
.populate('friends')
.setPopulateOptions({ limit: 2 }, ['limit', 'skip']);
You can also load the submodule that will give will take a populate value (e.g. friends
or { path: 'friends', options: { limit: 10 } }
and return an object that has the new options inserted at every level. This may be useful when manually dealing with options, like within an API.
const populateObfForElement = require('mongoose-populate-options/populateObfForElement');
const newPopulate = populateObfForElement(myOptions.populate, { limit: 5 });
npm test
./node_modules/.bin/eslint .
Makinde Adeagbo
This project is licensed under the MIT License - see the LICENSE file for details