Skip to content

Commit

Permalink
fix(schema): enforce that _id is never null
Browse files Browse the repository at this point in the history
Re: #5236 mongodb/node-mongodb-native#517
  • Loading branch information
vkarpov15 committed May 20, 2017
1 parent d951bed commit 5038635
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ Model.prototype.$__handleSave = function(options, callback) {

var obj = this.toObject(toObjectOptions);

if (!utils.object.hasOwnProperty(obj || {}, '_id')) {
if ((obj || {})._id == null) {
// documents must have an _id else mongoose won't know
// what to update later if more changes are made. the user
// wouldn't know what _id was generated by mongodb either
Expand Down
3 changes: 3 additions & 0 deletions lib/schema/objectid.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ function defaultId() {
}

function resetId(v) {
if (v == null) {
return new oid();
}
this.$__._id = null;
return v;
}
Expand Down
10 changes: 10 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4162,6 +4162,16 @@ describe('document', function() {
});
});

it('null _id (gh-5236)', function(done) {
var childSchema = new mongoose.Schema({});

var M = db.model('gh5236', childSchema);

var m = new M({ _id: null });
assert.ok(m._id);
done();
});

it('modify multiple subdoc paths (gh-4405)', function(done) {
var ChildObjectSchema = new Schema({
childProperty1: String,
Expand Down

0 comments on commit 5038635

Please sign in to comment.