mongoose-populate-virtuals
v1.0.3
Published
Extend Mongoose 4+ population with virtual attributes that can be populated in either direction.
Maintainers
Readme
mongoose-populate-virtuals
Extend Mongoose 4+ population with virtual attributes that can be populated in either direction.
Usage
Wrap mongoose:
var mongoose = require('mongoose-populate-virtuals')(require('mongoose'));Create document references to populate by defining
virtual attributes with
ref, localKey and foreignKey options.
Author.virtual('books', {
ref: 'Book',
foreignKey: 'authorId',
localKey: '_id'
});
Author.find().populate('books').exec(...);Remember virtual attributes
are not persisted to the DB. Virtuals are not included in the model's
.toObject()
or .toJSON() methods unless the options include { virtuals: true }.
Options
Options for populate virtuals:
refName of the mongoose model (required).foreignKeyKey on the model of the populated virtual (required).localKeyKey on the model being populated (required).matchQuery used forfind()orfineOne.optionsMongo options such assortandlimit.selectMongoose's.select()argument.singularUse singular reference instead of array.
