Skip to content

Commit

Permalink
Merge pull request #12023 from MitchellCash/docs_desc_index
Browse files Browse the repository at this point in the history
Add path level descending index example in docs
  • Loading branch information
vkarpov15 committed Jul 2, 2022
2 parents 8eb0eb4 + 3c1b46e commit 43126c5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
6 changes: 4 additions & 2 deletions docs/guide.md
Expand Up @@ -252,20 +252,22 @@ Animal.findOne().byName('fido').exec((err, animal) => {
<h3 id="indexes"><a href="#indexes">Indexes</a></h3>

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/).

```javascript
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).
Expand Down
3 changes: 2 additions & 1 deletion lib/schematype.js
Expand Up @@ -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 }})
Expand All @@ -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
*/
Expand Down

0 comments on commit 43126c5

Please sign in to comment.