Closed
Description
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 toID
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 accessannouncementCount
after populating it.