Skip to content

Commit bad9615

Browse files
committed
fix: id won't override _id
1 parent e293fe2 commit bad9615

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/document.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,9 @@ Document.prototype.overwrite = function overwrite(obj) {
10201020
*/
10211021

10221022
Document.prototype.$set = function $set(path, val, type, options) {
1023+
if (path._id && path.id) {
1024+
delete path.id;
1025+
}
10231026
if (utils.isPOJO(type)) {
10241027
options = type;
10251028
type = undefined;

test/document.test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12413,14 +12413,29 @@ describe('document', function() {
1241312413
_id: {
1241412414
type: Number
1241512415
}
12416-
}, { _id: false, id: false });
12416+
});
1241712417
const Test = db.model('Test', testSchema);
1241812418

1241912419
const x = new Test({ _id: 1, id: 2});
1242012420
await x.save();
1242112421
const fromDb = await Test.findById(x._id).lean();
1242212422
assert.equal(fromDb._id, 1);
1242312423
});
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+
})
1242412439
});
1242512440

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

0 commit comments

Comments
 (0)