Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions lib/helpers/populate/getSchemaTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ module.exports = function getSchemaTypes(model, schema, doc, path) {
}

const fullPath = nestedPath.concat([trypath]).join('.');
if (topLevelDoc != null && topLevelDoc.$__ && topLevelDoc.$populated(fullPath) && p < parts.length) {
const model = doc.$__.populated[fullPath].options[populateModelSymbol];
if (topLevelDoc != null && topLevelDoc.$__ && topLevelDoc.$populated(fullPath, true) && p < parts.length) {
const model = topLevelDoc.$populated(fullPath, true).options[populateModelSymbol];
if (model != null) {
const ret = search(
parts.slice(p),
Expand Down
37 changes: 37 additions & 0 deletions test/document.populate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1115,4 +1115,41 @@ describe('document.populate', function() {
assert.equal(docD.refB.title, 'test 2');
assert.equal(docD.refC.content, 'test 3');
});

it('handles re-populating map of array of refs (gh-9359)', async function() {
const UserSchema = mongoose.Schema({
columns: { type: Map, of: [{ type: 'ObjectId', ref: 'Test1' }] }
});
const CardSchema = mongoose.Schema({
title: { type: String },
sequence: { type: 'ObjectId', ref: 'Test2' }
});
const SequenceSchema = mongoose.Schema({
foo: { type: String }
});

const Sequence = db.model('Test2', SequenceSchema);
const Card = db.model('Test1', CardSchema);
const User = db.model('Test', UserSchema);

const sequence = await Sequence.create({ foo: 'bar' });
const card1 = await Card.create({ title: 'card1', sequence });
const card2 = await Card.create({ title: 'card2', sequence });
const card3 = await Card.create({ title: 'card3' });
const card4 = await Card.create({ title: 'card4', sequence });
await User.create({
columns: { key1: [card1, card2], key2: [card3, card4] }
});

const user = await User.findOne();
await user.populate('columns.$*');
assert.deepStrictEqual(user.columns.get('key1').map(subdoc => subdoc.title), ['card1', 'card2']);
assert.deepStrictEqual(user.columns.get('key2').map(subdoc => subdoc.title), ['card3', 'card4']);
await user.populate('columns.$*.sequence');
assert.deepStrictEqual(user.columns.get('key1').map(subdoc => subdoc.title), ['card1', 'card2']);
assert.deepStrictEqual(user.columns.get('key1').map(subdoc => subdoc.sequence.foo), ['bar', 'bar']);
assert.deepStrictEqual(user.columns.get('key2').map(subdoc => subdoc.title), ['card3', 'card4']);
assert.deepStrictEqual(user.columns.get('key2').map(subdoc => subdoc.sequence?.foo), [undefined, 'bar']);

});
});
Loading