diff --git a/lib/helpers/document/applyDefaults.js b/lib/helpers/document/applyDefaults.js index 258e570ec30..7d1b5b02382 100644 --- a/lib/helpers/document/applyDefaults.js +++ b/lib/helpers/document/applyDefaults.js @@ -19,6 +19,9 @@ module.exports = function applyDefaults(doc, fields, exclude, hasIncludedChildre const type = doc.$__schema.paths[p]; const path = type.splitPath(); const len = path.length; + if (path[len - 1] === '$*') { + continue; + } let included = false; let doc_ = doc._doc; for (let j = 0; j < len; ++j) { diff --git a/test/types.map.test.js b/test/types.map.test.js index c6486e507ab..a61850fa9a6 100644 --- a/test/types.map.test.js +++ b/test/types.map.test.js @@ -1150,4 +1150,33 @@ describe('Map', function() { const doc = await CarModel.findById(car._id); assert.deepStrictEqual(doc.owners.get('abc').toObject(), [{ name: 'Bill' }]); }); + + it('handles loading and modifying map of document arrays (gh-15196)', async function() { + const schema = new Schema({ + name: { type: String, required: true }, + test_map: { + type: Map, + of: [{ + _id: false, + num: { type: Number, required: true }, + bool: { type: Boolean, required: true } + }] + } + }); + const Test = db.model('Test', schema); + + let doc1 = new Test({ name: 'name1', test_map: new Map() }); + await doc1.save(); + + doc1 = await Test.findOne({ _id: doc1._id }); + + doc1.test_map.set('key1', []); + await doc1.save(); + + doc1 = await Test.findOne({ _id: doc1._id }); + assert.deepStrictEqual(doc1.toObject().test_map, new Map([['key1', []]])); + + doc1 = await Test.findOne({ _id: doc1._id }).lean(); + assert.deepStrictEqual(doc1.test_map, { key1: [] }); + }); });