Skip to content

.populate() on virtual fields using UUID arrays leads to empty array #15382

Closed
@lhimstedt

Description

@lhimstedt

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

  1. Define a schema with UUIDs array attribute like role_ids.
  2. Add a virtual to the schema using the previous field from step 1 as local field and reference an UUID as foreign field.
  3. Add documents in both collections, the referencing and the referenced one.
  4. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions