Skip to content

Commit

Permalink
fix(setDefaultsOnInsert): set default if sibling of nested path is $set
Browse files Browse the repository at this point in the history
Fix #11668
  • Loading branch information
vkarpov15 committed Apr 27, 2022
1 parent c3024b9 commit 25c7bce
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
8 changes: 0 additions & 8 deletions lib/helpers/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,6 @@ function modifiedPaths(update, path, result) {

const _path = path + key;
result[_path] = true;
if (_path.indexOf('.') !== -1) {
const sp = _path.split('.');
let cur = sp[0];
for (let i = 1; i < sp.length; ++i) {
result[cur] = true;
cur += '.' + sp[i];
}
}
if (!Buffer.isBuffer(val) && isMongooseObject(val)) {
val = val.toObject({ transform: false, virtuals: false });
}
Expand Down
15 changes: 15 additions & 0 deletions test/helpers/setDefaultsOnInsert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,19 @@ describe('setDefaultsOnInsert', function() {
assert.ok(!update.$setOnInsert['positions.$*']);
assert.ok(update.$setOnInsert['positions'] instanceof Map);
});

it('sets default if sibling of dotted path is $set (gh-11668)', async function() {
const schema = new Schema({
time: {
hit: { type: Number, default: 0 },
resolved: { type: Number, default: 42 }
}
});

const opts = { upsert: true };
let update = { $set: { 'time.hit': 100 } };
update = setDefaultsOnInsert({}, schema, update, opts);

assert.equal(update.$setOnInsert['time.resolved'], 42);
});
});

0 comments on commit 25c7bce

Please sign in to comment.