Closed
Description
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Mongoose version
= 8.13.1
Node.js version
22.14.0
MongoDB server version
7.0.14
Typescript version (if applicable)
5.7.3
Description
I am using virtual fields with populate in my application. Everything worked fine in the past but with the update to 8.13.1 or all follow-up versions the .populate() breaks. The virtual field is based on an array of UUIDs.
Here the code for the schema:
const schema = new Schema(
{
_id: {
type: SchemaTypes.UUID,
required: true,
},
_role_ids: [
{
type: SchemaTypes.UUID,
ref: 'Role',
required: true,
},
],
},
{
toObject: {
virtuals: true,
},
toJSON: {
virtuals: true,
transform: (doc: IMemberDocument, ret) => {
return mongooseUtils.toJSON(doc, ret);
},
},
}
);
And for the virtual field:
schema.virtual('roles', {
ref: 'Role',
localField: '_role_ids',
foreignField: '_id',
justOne: false,
});
The roles exist in the database, when retrieving them manually using .find() it returns them correctly.
The part with the .populate() call:
Member.findOneAndUpdate(
{
_id: req.params.memberId,
_user_id: req.user?.user_id,
},
{
_role_ids: req.body.roleIds,
},
{new: true, session}
).populate('roles')
Before the update it worked.
Steps to Reproduce
- Define a schema with UUIDs array attribute like
role_ids
. - Add a virtual to the schema using the previous field from step 1 as local field and reference an UUID as foreign field.
- Add documents in both collections, the referencing and the referenced one.
- Use .populate() on the virtual field.
Behaviour: The populated array is empty. Even if documents exist.
Expected Behavior
It returns an array of populated documents.