0.0.4 • Published 8 years ago
mongoose-populate-extended v0.0.4
Mongoose 4+ plugin to perform population into schema's virtual property.
Description
Mongoose ODM population does not provide an ability to populate documents into custom defined virtual property. This plugin enchances population functionality to provide such.
Plugin defines a new schema's property definition option asVirtual
that contains a key reference to the virtual property that will be populated. After population is performed the original reference is left intact.
Usage
Learn by example
var mongoose = require("mongoose");
// define schema
var schema = new Schema({
propId: {
type: mongoose.Schema.Types.ObjectId,
ref: "Reference",
asVirtual: "prop" // populate into `prop` virtual property
}
})
// register plugin
var populateExtended = require("mongoose-populate-extended")(mongoose);
schema.plugin(populateExtended);
// define schema's model
var Model = mongoose.model("Model", schema);
Perform population
On Model
model:
Model.populate(models, "propId", function (err, _models) {
// _models are equal to models and are populated with propId reference
_models.forEach(function (model) {
// model.propId is left intact
// model.prop contains populated document
});
})
On an instance of Model
:
model.populate("propId", function (err, _model) {
// _model is equal to model and is populated with propId reference
// _model.propId is left intact
// _model.prop contains populated document
})
On a Query
:
Model.find().populate("propId").exec(function (err, models) { ... });
Model.findOne().populate("propId").exec(function (err, model) { ... });
Changelog
v0.0.4
- Bugfixing
v0.0.1
- Initial release
License
MIT