Skip to content

Commit be41ea5

Browse files
authored
Merge pull request #12321 from Automattic/vkarpov15/gh-12264
Make ArraySubdocuments apply `_id` defaults on init
2 parents ff965da + 4a946ff commit be41ea5

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/schema/documentarray.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ DocumentArrayPath.prototype.getDefault = function(scope, init, options) {
392392
};
393393

394394
const _toObjectOptions = Object.freeze({ transform: false, virtuals: false });
395-
const initDocumentOptions = Object.freeze({ skipId: true, willInit: true });
395+
const initDocumentOptions = Object.freeze({ skipId: false, willInit: true });
396396

397397
/**
398398
* Casts contents

test/types.documentarray.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,4 +748,18 @@ describe('types.documentarray', function() {
748748
doc.arr.push(subdoc);
749749
await doc.validate();
750750
});
751+
752+
it('applies _id default (gh-12264)', function() {
753+
mongoose.deleteModel(/Test/);
754+
const nestedArraySchema = Schema({
755+
subDocArray: [{ name: String }]
756+
});
757+
758+
const Model = db.model('Test', nestedArraySchema);
759+
const doc = new Model().init({
760+
subDocArray: [{ name: 'foo' }]
761+
});
762+
763+
assert.ok(doc.subDocArray[0]._id instanceof mongoose.Types.ObjectId);
764+
});
751765
});

0 commit comments

Comments
 (0)