Skip to content

Commit

Permalink
Merge pull request #13854 from Automattic/IslandRhythms/gh-13762
Browse files Browse the repository at this point in the history
Island rhythms/gh 13762
  • Loading branch information
vkarpov15 committed Sep 14, 2023
2 parents 71bbf64 + a6d4213 commit 87619d0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,10 @@ Document.prototype.$set = function $set(path, val, type, options) {
[path, val] = [val, path];
}

if ('_id' in path && 'id' in path) {
delete path.id;
}

prefix = val ? val + '.' : '';
keys = getKeysInSchemaOrder(this.$__schema, path);

Expand Down
29 changes: 29 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12407,6 +12407,35 @@ describe('document', function() {
const nestedProjectionDoc = await User.findOne({}, { name: 1, 'sub.propertyA': 1, 'sub.propertyB': 1 });
assert.strictEqual(nestedProjectionDoc.sub.propertyA, 'A');
});

it('should ignore `id` if the object contains `id` and `_id` as keys (gh-13762)', async function() {
const testSchema = new Schema({
_id: {
type: Number
}
});
const Test = db.model('Test', testSchema);

const x = new Test({ _id: 1, id: 2 });
await x.save();
const fromDb = await Test.findById(x._id).lean();
assert.equal(fromDb._id, 1);
});
it('should ignore `id` if settings with an object that contains `_id` and `id` (gh-13672)', async function() {
const testSchema = new Schema({
_id: {
type: Number
}
});
const Test = db.model('Test', testSchema);

const x = new Test();
x.set('_id', { _id: 1, id: 2 });
await x.save();

const fromDb = await Test.findById(x._id).lean();
assert.equal(fromDb._id, 1);
});
});

describe('Check if instance function that is supplied in schema option is availabe', function() {
Expand Down

0 comments on commit 87619d0

Please sign in to comment.