-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic Object ID reference for a subdocument inside an array #14249
Labels
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
Milestone
Comments
IslandRhythms
added
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
and removed
note to self
labels
Feb 8, 2024
Populating both results in only catB being populated. If only one entry, it works as intended. const mongoose = require('mongoose');
const { Schema } = mongoose;
const aSchema = new Schema({
name: String
});
const bSchema = new Schema({
name: String
});
const CategoryAModel = mongoose.model('CategoryAModel', aSchema);
const CategoryBModel = mongoose.model('CategoryBModel', bSchema);
const testSchema = new Schema({
category: String,
ref: {
type: Schema.Types.ObjectId,
ref: function() {
return this.category === 'catA' ? CategoryAModel : CategoryBModel
}
}
});
const parentSchema = new Schema({
name: String,
children: [testSchema]
});
const Test = mongoose.model('Test', parentSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
const A = await CategoryAModel.create({
name: 'A'
});
const B = await CategoryBModel.create({
name: 'B'
});
const doc = await Test.create({
name: 'Parent',
children: [{ category: 'catA', ref: A._id }, { category: 'catB', ref: B._id }]
});
const res = await Test.findOne().populate('children.ref');
console.log('what is res', res, res.children);
}
run(); |
vkarpov15
added a commit
that referenced
this issue
Feb 9, 2024
vkarpov15
added a commit
that referenced
this issue
Feb 12, 2024
fix(populate): handle ref() functions that return a model instance
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Discussed in #14243
Originally posted by echo-999 January 8, 2024
I'm trying to create a dynamic ref for an object ID stored inside a property of a subdocument. The property can reference multiple models (even models registered in a different mongoose database instance) so I use the model instance directly instead of a model name.
As you can see, the
ref
property is the Object ID of either a document fromCategoryAModel
orCategoryBModel
. I started creating documents for this model, such as the following:But when i tried to
populate
this, theref
forcategory: 'catA'
becomes null (despite existing). I logged thethis
context in theref
's ref function and saw thatthis
refers to the document being processed (same shape as the data above), and thatthis.category
is undefined because it's actually inside thechildren
array. Essentially making theref
always result in being theCategoryBModel
Since it's an array, how would I go and make a dynamic reference? Is there a way to access the index of the
subSchema
being referred to?The text was updated successfully, but these errors were encountered: