I have the following Schemas:
var TripSchema = new mongoose.Schema({
...
});
TripSchema.virtual('Orders', {
ref: 'Order',
localField: '_id',
foreignField: 'trip_id'
});
var OrderSchema = new mongoose.Schema({
'trip_id': {type: mongoose.Schema.Types.ObjectId, required: true},
'vehicle_id': {type: mongoose.Schema.Types.ObjectId, required: true},
....
});
OrderSchema.virtual('Vehicle', {
ref: 'Vehicle',
localField: 'vehicle_id',
foreignField: '_id',
justOne: true
});
var VehicleSchema = new mongoose.Schema({
...
});
var Order = mongoose.model('Order', OrderSchema);
var Trip = mongoose.model('Trip', TripSchema);
var Vehicle = mongoose.model('Vehicle', VehicleSchema);
And i'm trying to populate it querying from a Trip model:
var self = this; // <-- suppose we are inside a TripSchema.statics.someFuncName
var trip_id = "XXXXXXXXXXXXXXXXXX";
var populate = [{
path: 'Orders',
populate: {
path: 'Vehicle'
}
}];
self.findById(trip_id).populate(populate).exec(function(err, trip) {
// trip.Orders is correctly populated with an array of Order as expected
// trip.Orders[x].Vehicle is not populated
}
But is not properly populated.
Oviusly if I try to populate 'vehicle_id' instead of 'Vehicle' it works, but i want to use the Virtuals if possible.
Also i didn't find the documentation about this use of Virtuals anywhere.
Thanks.
Related:
The features works. It was a problem related to a thing external to mongoose.
I close the issue.
@vkarpov15 I think this could be useful for someone as example of how to use nested virtuals. I think that something like this should be added to the documentation.
Regards,
Simone
Most helpful comment
The features works. It was a problem related to a thing external to mongoose.
I close the issue.
@vkarpov15 I think this could be useful for someone as example of how to use nested virtuals. I think that something like this should be added to the documentation.
Regards,
Simone