Skip to content

Commit 87619d0

Browse files
authored
Merge pull request #13854 from Automattic/IslandRhythms/gh-13762
Island rhythms/gh 13762
2 parents 71bbf64 + a6d4213 commit 87619d0

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

lib/document.js

+4
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,10 @@ Document.prototype.$set = function $set(path, val, type, options) {
10601060
[path, val] = [val, path];
10611061
}
10621062

1063+
if ('_id' in path && 'id' in path) {
1064+
delete path.id;
1065+
}
1066+
10631067
prefix = val ? val + '.' : '';
10641068
keys = getKeysInSchemaOrder(this.$__schema, path);
10651069

test/document.test.js

+29
Original file line numberDiff line numberDiff line change
@@ -12407,6 +12407,35 @@ describe('document', function() {
1240712407
const nestedProjectionDoc = await User.findOne({}, { name: 1, 'sub.propertyA': 1, 'sub.propertyB': 1 });
1240812408
assert.strictEqual(nestedProjectionDoc.sub.propertyA, 'A');
1240912409
});
12410+
12411+
it('should ignore `id` if the object contains `id` and `_id` as keys (gh-13762)', async function() {
12412+
const testSchema = new Schema({
12413+
_id: {
12414+
type: Number
12415+
}
12416+
});
12417+
const Test = db.model('Test', testSchema);
12418+
12419+
const x = new Test({ _id: 1, id: 2 });
12420+
await x.save();
12421+
const fromDb = await Test.findById(x._id).lean();
12422+
assert.equal(fromDb._id, 1);
12423+
});
12424+
it('should ignore `id` if settings with an object that contains `_id` and `id` (gh-13672)', async function() {
12425+
const testSchema = new Schema({
12426+
_id: {
12427+
type: Number
12428+
}
12429+
});
12430+
const Test = db.model('Test', testSchema);
12431+
12432+
const x = new Test();
12433+
x.set('_id', { _id: 1, id: 2 });
12434+
await x.save();
12435+
12436+
const fromDb = await Test.findById(x._id).lean();
12437+
assert.equal(fromDb._id, 1);
12438+
});
1241012439
});
1241112440

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

0 commit comments

Comments
 (0)