You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
the model being "populated" is not an explicitly defined sub-type
Reproduction
This script creates a models for Animal and Food. All animals have a type (cat | rabbit). All animals have a virtual that populates the Food they eat. An Animal of kind "cat" has a custom catYears property. An Animal of kind "rabbit" has no rabbit-specific props.
A cat and a rabbit are populated along with food for each of them. Both are then fetched from the DB, populating the foods they eat. The result is logged.
The expectation is that the foods for both are populated. However, it is only populated for the cat (which has a discriminator set up). If we add a discriminator for "rabbit", we will see it is populated for both.
// index.js// @ts-checkconstmongoose=require("mongoose")const{ MongoMemoryServer}=require("mongodb-memory-server");(async()=>{/** * SETUP DATABASE */constmongod=newMongoMemoryServer();consturi=awaitmongod.getUri();/** * CONNECT MONGOOSE */awaitmongoose.connect(uri,{useNewUrlParser: true,useUnifiedTopology: true});/** * CREATE SCHEMAS */constfoodSchema=newmongoose.Schema({name: String,animal: String});constFood=mongoose.model('Food',foodSchema);constanimalSchema=newmongoose.Schema({type: String,},{discriminatorKey: 'type',toJSON: {virtuals: true},toObject: {virtuals: true}});constcatSchema=newmongoose.Schema({catYears: {type: Number,required: true}});animalSchema.virtual('foods',{ref: 'Food',localField: 'type',foreignField: 'animal',justOne: false});constAnimal=mongoose.model('Animal',animalSchema);constCat=Animal.discriminator('cat',catSchema)/** * TEST */awaitPromise.all([newFood({name: 'Cat Food',animal: 'cat'}).save(),newFood({name: 'Rabbit Food',animal: 'rabbit'}).save(),]);awaitnewAnimal({type: 'cat',catYears: 4}).save();awaitnewAnimal({type: 'rabbit'}).save();// <-- "rabbit" has no discriminator constcat=awaitAnimal.findOne({type: 'cat'}).populate('foods');constrabbit=awaitAnimal.findOne({type: 'rabbit'}).populate('foods');/** * Expect: Both cat and rabbit have "foods" * Result: Only cat has "foods" */console.log(JSON.stringify({ cat, rabbit },undefined,2));})().catch(err=>console.error(err)).then(()=>process.exit())
Expected Behaviour
Populate Virtuals work irrespective of if a specific instance of a model has specific sub-type defined.
Use Cases
The use cases for this are where there are models with a number of variants, but only a small number of those variants have variant-specific behaviour.
Versions
Node: v12.21.0 / v14.8.0
Mongoose: 5.12.2
The text was updated successfully, but these errors were encountered:
Issue: Bug
Behaviour:
Populate Virtuals do not work when there is:
Reproduction
This script creates a models for
Animal
andFood
. All animals have a type (cat | rabbit
). All animals have a virtual that populates theFood
they eat. AnAnimal
of kind "cat" has a customcatYears
property. AnAnimal
of kind "rabbit" has no rabbit-specific props.A cat and a rabbit are populated along with food for each of them. Both are then fetched from the DB, populating the foods they eat. The result is logged.
The expectation is that the foods for both are populated. However, it is only populated for the cat (which has a discriminator set up). If we add a discriminator for "rabbit", we will see it is populated for both.
Expected Behaviour
Populate Virtuals work irrespective of if a specific instance of a model has specific sub-type defined.
Use Cases
The use cases for this are where there are models with a number of variants, but only a small number of those variants have variant-specific behaviour.
Versions
Node: v12.21.0 / v14.8.0
Mongoose: 5.12.2
The text was updated successfully, but these errors were encountered: