Skip to content

fix(populate): handle virtual populate on array of UUIDs #15329

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

Merged
merged 4 commits into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/helpers/populate/assignRawDocsToIdStructure.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ function assignRawDocsToIdStructure(rawIds, resultDocs, resultOrder, options, re
continue;
}

sid = String(id);
if (id?.constructor?.name === 'Binary' && id.sub_type === 4 && typeof id.toUUID === 'function') {
// Workaround for gh-15315 because Mongoose UUIDs don't use BSON UUIDs yet.
sid = String(id.toUUID());
} else {
sid = String(id);
}
doc = resultDocs[sid];
// If user wants separate copies of same doc, use this option
if (options.clone && doc != null) {
Expand Down
42 changes: 42 additions & 0 deletions test/model.populate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
const start = require('./common');

const assert = require('assert');
const { randomUUID } = require('crypto');
const utils = require('../lib/utils');
const util = require('./util');
const MongooseError = require('../lib/error/mongooseError');
Expand Down Expand Up @@ -11423,4 +11424,45 @@ describe('model: populate:', function() {

await m.disconnect();
});

it('handles populating UUID fields (gh-15315)', async function() {
const categorySchema = new Schema({
_id: { type: 'UUID', default: () => randomUUID() },
name: { type: String, required: true },
desc: { type: String, required: true }
});

categorySchema.virtual('announcements', {
ref: 'Announcement',
localField: '_id',
foreignField: 'categories'
});

const announcementSchema = new Schema({
_id: { type: 'UUID', default: () => randomUUID() },
title: { type: String, required: true },
content: { type: String, required: true },
validUntil: { type: Date, required: true },
important: { type: Boolean, default: false },
categories: [{ type: 'UUID', ref: 'Category' }]
});

const Category = db.model('Category', categorySchema);
const Announcement = db.model('Announcement', announcementSchema);

const category = new Category({ name: 'Tech', desc: 'Technology News' });
await category.save();

const announcement = new Announcement({
title: 'New Tech Release',
content: 'Details about the new tech release',
validUntil: new Date(),
categories: [category._id]
});
await announcement.save();

const populatedCategory = await Category.findOne({ _id: category._id }).populate('announcements');
assert.strictEqual(populatedCategory.announcements.length, 1);
assert.strictEqual(populatedCategory.announcements[0].title, 'New Tech Release');
});
});
Loading