diff --git a/lib/helpers/schema/getIndexes.js b/lib/helpers/schema/getIndexes.js index 899d309b5ed..877156a2878 100644 --- a/lib/helpers/schema/getIndexes.js +++ b/lib/helpers/schema/getIndexes.js @@ -36,7 +36,8 @@ module.exports = function getIndexes(schema) { if (path.$isMongooseDocumentArray || path.$isSingleNested) { if (get(path, 'options.excludeIndexes') !== true && - get(path, 'schemaOptions.excludeIndexes') !== true) { + get(path, 'schemaOptions.excludeIndexes') !== true && + get(path, 'schema.options.excludeIndexes') !== true) { collectIndexes(path.schema, prefix + key + '.'); } diff --git a/lib/options/SchemaDocumentArrayOptions.js b/lib/options/SchemaDocumentArrayOptions.js new file mode 100644 index 00000000000..0a17a87e5e4 --- /dev/null +++ b/lib/options/SchemaDocumentArrayOptions.js @@ -0,0 +1,48 @@ +'use strict'; + +const SchemaTypeOptions = require('./SchemaTypeOptions'); + +/** + * The options defined on an Document Array schematype. + * + * ####Example: + * + * const schema = new Schema({ users: [{ name: string }] }); + * schema.path('users').options; // SchemaDocumentArrayOptions instance + * + * @api public + * @inherits SchemaTypeOptions + * @constructor SchemaDocumentOptions + */ + +class SchemaDocumentArrayOptions extends SchemaTypeOptions {} + +const opts = require('./propertyOptions'); + +/** + * If `true`, Mongoose will skip building any indexes defined in this array's schema. + * If not set, Mongoose will build all indexes defined in this array's schema. + * + * ####Example: + * + * const childSchema = Schema({ name: { type: String, index: true } }); + * // If `excludeIndexes` is `true`, Mongoose will skip building an index + * // on `arr.name`. Otherwise, Mongoose will build an index on `arr.name`. + * const parentSchema = Schema({ + * arr: { type: [childSchema], excludeIndexes: true } + * }); + * + * @api public + * @property excludeIndexes + * @memberOf SchemaDocumentArrayOptions + * @type Array + * @instance + */ + +Object.defineProperty(SchemaDocumentArrayOptions.prototype, 'excludeIndexes', opts); + +/*! + * ignore + */ + +module.exports = SchemaArrayOptions; \ No newline at end of file diff --git a/lib/schema.js b/lib/schema.js index 2cbfe7da9b7..1aac5594522 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -72,6 +72,9 @@ let id = 0; * - [timestamps](/docs/guide.html#timestamps): object or boolean - defaults to `false`. If true, Mongoose adds `createdAt` and `updatedAt` properties to your schema and manages those properties for you. * - [storeSubdocValidationError](/docs/guide.html#storeSubdocValidationError): boolean - Defaults to true. If false, Mongoose will wrap validation errors in single nested document subpaths into a single validation error on the single nested subdoc's path. * + * ####Options for Nested Schemas: + * - `excludeIndexes`: bool - defaults to `false`. If `true`, skip building indexes on this schema's paths. + * * ####Note: * * _When nesting schemas, (`children` in the example above), always declare the child schema first before passing it into its parent._