diff --git a/lib/document.js b/lib/document.js index 9e5c474a701..2d72271f166 100644 --- a/lib/document.js +++ b/lib/document.js @@ -136,7 +136,6 @@ function Document(obj, fields, skipId, options) { // excluded fields if (utils.isPOJO(fields)) { exclude = isExclusive(fields); - this.$__.fields = fields; this.$__.exclude = exclude; } diff --git a/lib/options/SchemaTypeOptions.js b/lib/options/SchemaTypeOptions.js index 20a33213df2..22f08a09ce8 100644 --- a/lib/options/SchemaTypeOptions.js +++ b/lib/options/SchemaTypeOptions.js @@ -130,7 +130,7 @@ Object.defineProperty(SchemaTypeOptions.prototype, 'ref', opts); * @instance */ - Object.defineProperty(SchemaTypeOptions.prototype, 'refPath', opts); +Object.defineProperty(SchemaTypeOptions.prototype, 'refPath', opts); /** * Whether to include or exclude this path by default when loading documents diff --git a/lib/schema.js b/lib/schema.js index 6464e6c8b52..2d6e24c252a 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -1202,6 +1202,7 @@ Schema.prototype.interpretAsType = function(path, obj, options) { } if (type && type.instanceOfSchema) { + return new MongooseTypes.Subdocument(type, path, obj); } diff --git a/lib/schema/SubdocumentPath.js b/lib/schema/SubdocumentPath.js index 766d05fa292..94a8c588628 100644 --- a/lib/schema/SubdocumentPath.js +++ b/lib/schema/SubdocumentPath.js @@ -33,6 +33,13 @@ module.exports = SubdocumentPath; */ function SubdocumentPath(schema, path, options) { + const schemaTypeIdOption = SubdocumentPath.defaultOptions && + SubdocumentPath.defaultOptions._id; + if (schemaTypeIdOption != null) { + options = options || {}; + options._id = schemaTypeIdOption; + } + schema = handleIdOption(schema, options); this.caster = _createConstructor(schema); diff --git a/test/common.js b/test/common.js index 175a8c391bd..234c123590e 100644 --- a/test/common.js +++ b/test/common.js @@ -84,6 +84,7 @@ module.exports = function(options) { const noErrorListener = !!options.noErrorListener; delete options.noErrorListener; + options.enableUtf8Validation = false; const conn = mongoose.createConnection(uri, options); diff --git a/test/schema.subdocumentpath.test.js b/test/schema.subdocumentpath.test.js index 7fa2f87d5dc..feadcc0273a 100644 --- a/test/schema.subdocumentpath.test.js +++ b/test/schema.subdocumentpath.test.js @@ -181,4 +181,22 @@ describe('SubdocumentPath', function() { mongoose.Schema.Types.Subdocument.set('required', false); }); + + it('supports setting _id globally (gh-11541) (gh-8883)', function() { + mongoose.deleteModel(/Test/); + mongoose.Schema.Types.Subdocument.set('_id', false); + + const Model = mongoose.model('Test', mongoose.Schema({ + nested: mongoose.Schema({ + test: String + }) + })); + + const doc = new Model({ nested: {} }); + + assert.ok(!doc.nested._id); + + delete mongoose.Schema.Types.Subdocument.defaultOptions._id; + mongoose.Schema.Types.Subdocument.set('required', false); + }); }); \ No newline at end of file