From 4e8ae4b6a8799cccccd9250911ad48cd74a9b627 Mon Sep 17 00:00:00 2001 From: Mitchell Cash Date: Thu, 30 Jun 2022 14:26:57 +0100 Subject: [PATCH 1/2] Add path level descending index example in docs --- docs/guide.md | 4 +++- lib/schematype.js | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/guide.md b/docs/guide.md index ed64cc5a521..fabf1d31b9e 100644 --- a/docs/guide.md +++ b/docs/guide.md @@ -260,12 +260,14 @@ Defining indexes at the schema level is necessary when creating const animalSchema = new Schema({ name: String, type: String, - tags: { type: [String], index: true } // field level + tags: { type: [String], index: true } // path level }); animalSchema.index({ name: 1, type: -1 }); // schema level ``` +See [SchemaType#index()](./api.html#schematype_SchemaType-index) for other index options. + When your application starts up, Mongoose automatically calls [`createIndex`](https://docs.mongodb.com/manual/reference/method/db.collection.createIndex/#db.collection.createIndex) for each defined index in your schema. Mongoose will call `createIndex` for each index sequentially, and emit an 'index' event on the model when all the `createIndex` calls succeeded or when there was an error. While nice for development, it is recommended this behavior be disabled in production since index creation can cause a [significant performance impact](https://docs.mongodb.com/manual/core/index-creation/#index-build-impact-on-database-performance). diff --git a/lib/schematype.js b/lib/schematype.js index 53a687370d4..5e1cc23f254 100644 --- a/lib/schematype.js +++ b/lib/schematype.js @@ -384,6 +384,7 @@ SchemaType.prototype.default = function(val) { * #### Example: * * const s = new Schema({ name: { type: String, index: true }) + * const s = new Schema({ name: { type: String, index: -1 }) * const s = new Schema({ loc: { type: [Number], index: 'hashed' }) * const s = new Schema({ loc: { type: [Number], index: '2d', sparse: true }) * const s = new Schema({ loc: { type: [Number], index: { type: '2dsphere', sparse: true }}) @@ -399,7 +400,7 @@ SchemaType.prototype.default = function(val) { * read/write operations you send until the index build. * Specify `background: false` to override Mongoose's default._ * - * @param {Object|Boolean|String} options + * @param {Object|Boolean|String|Number} options * @return {SchemaType} this * @api public */ From 3c1b46e9799eda6611fc99b0a4b8d7ceaa21862c Mon Sep 17 00:00:00 2001 From: Mitchell Cash Date: Thu, 30 Jun 2022 14:31:02 +0100 Subject: [PATCH 2/2] Fix broken SchemaDateOptions#expires link --- docs/guide.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/guide.md b/docs/guide.md index fabf1d31b9e..624cc658836 100644 --- a/docs/guide.md +++ b/docs/guide.md @@ -252,7 +252,7 @@ Animal.findOne().byName('fido').exec((err, animal) => {

Indexes

MongoDB supports [secondary indexes](http://docs.mongodb.org/manual/indexes/). -With mongoose, we define these indexes within our `Schema` [at](./api.html#schematype_SchemaType-index) [the](./api.html#schematype_SchemaType-unique) [path](./api.html#schematype_SchemaType-sparse) [level](./api.html#schema_date_SchemaDate-expires) or the `schema` level. +With mongoose, we define these indexes within our `Schema` [at](./api.html#schematype_SchemaType-index) [the](./api.html#schematype_SchemaType-unique) [path](./api.html#schematype_SchemaType-sparse) [level](./api.html#schemadateoptions_SchemaDateOptions-expires) or the `schema` level. Defining indexes at the schema level is necessary when creating [compound indexes](https://docs.mongodb.com/manual/core/index-compound/).