Reputation: 2453
I'm trying to figure out how to populate my business orders using the businessId property in my orders collection.
I tried this but can't get it work. https://www.npmjs.com/package/mongoose-reverse-populate
Any idea of what I'm doing wrong or other suggestions for how to accomplish what I need to do?
Business.findById(id).exec(function(err, business) {
var opts = {
modelArray: business,
storeWhere: "orders",
arrayPop: true,
mongooseModel: Order,
idField: "businessId"
};
reversePopulate(opts, function(err, businessAndOrders) {
req.business = businessAndOrders;
next();
});
});
--
var BusinessSchema = new Schema({
businessName:{
type:String
}
/*.......*/
});
var OrderSchema = new Schema({
data: {
type: String,
}
created: {
type: Date,
default: Date.now
},
businessId: {
type: Schema.ObjectId,
ref: 'Business'
}
});
Upvotes: 4
Views: 2235
Reputation: 191
You can use Populate Virtuals to achieve reverse populate. (Mongoose version > 4.5.0)
For case please refer official doc as link above or https://stackoverflow.com/a/51664307/3210050
Upvotes: 1