Skip to content

Virtual Populate foreignField is array in combination with UUIDs #15315

Closed
@JustDoItSascha

Description

@JustDoItSascha

Hello,

I have the exact same issue @Namchee had in this other issue. The query which Mongoose performs is correct, it would return the correct documents from the foreign schema. But it seems like it's not getting merged correctly afterwards...

Problem exists with Mongoose 8.12.0

P.S. I found out, it's only happening when using UUIDs instead of ObjectId.

All of the cases here refers to array of IDs in virtualized-schema to ID in reference schema. But, what if I have the opposite case?

Example:

Announcement Schema


const announcementSchema = new Schema({

  title: {

    type: String,

    trim: true,

    unique: true,

    required: true,

    maxlength: 25,

  },



  content: {

    type: String,

    trim: true,

    required: true,

    maxlength: 500,

  },



  validUntil: {

    type: Date,

    required: true,

  },



  important: {

    type: Boolean,

    default: false,

  },



  categories: {

    type: [Schema.Types.ObjectId],

    ref: 'Category',

    default: [],

  },

});

Category Schema


const categorySchema = new Schema({

  name: {

    type: String,

    trim: true,

    required: true,

    index: true,

    maxlength: 50,

  },



  desc: {

    type: String,

    trim: true,

    required: true,

    maxlength: 250,

  },

});



categorySchema.virtual('announcementCount', {

  ref: 'Announcement',

  localField: 'id',

  foreignField: 'categories',

  count: true,

});

I've tried the above solution, but I keep getting undefined when I tried to access announcementCount after populating it.

Originally posted by @Namchee in #4585

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