Skip to content
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
2 changes: 2 additions & 0 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -1517,6 +1517,8 @@ Document.prototype.$set = function $set(path, val, type, options) {

if (schema.$isSingleNested && (this.isDirectModified(path) || val == null)) {
cleanModifiedSubpaths(this, path);
} else if (schema.$isSchemaMap && val == null) {
cleanModifiedSubpaths(this, path);
}

return this;
Expand Down
27 changes: 27 additions & 0 deletions test/types.map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1347,4 +1347,31 @@ describe('Map', function() {
assert.ok(error);
assert.strictEqual(error.errors['a.b.c.d.e'].message.includes('Path `e`'), true);
});

it('handles setting then unsetting the same map (gh-15519)', async function() {
const debugSchema = new Schema({
settings: {
type: Map,
of: String
}
});

const Debug = db.model('Test', debugSchema);
const empty = new Debug();
await empty.save();

let doc = await Debug.findById(empty._id);
doc.settings = new Map();

doc.settings.set('test', 'value');

doc.settings = undefined;
assert.deepStrictEqual(doc.getChanges(), { $unset: { settings: 1 } });

await doc.save();

// reload and check settings is undefined
doc = await Debug.findById(empty._id);
assert.strictEqual(doc.settings, undefined);
});
});