I am posting this question and answer in the hope that it will help someone else (or if there is a better answer).
How to create virtual objects for nested Mongoose schemas in array form?
Here are the diagrams:
var Variation = new Schema({ label: { type: String } }); var Product = new Schema({ title: { type: String } variations: { type: [Variation] } });
As I would like virtual on variations . It seems that if the sub doc is not an array, we can simply do this:
Product.virtual('variations.name')...
But this only works for non-arrays.
source share