Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add path level descending index example in docs #12023

Merged
merged 2 commits into from Jul 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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