Skip to main content

@index

Typings:

function index<T extends BeAnObject = BeAnObject>(fields: mongoose.IndexDefinition, options?: IndexOptions<T>): ClassDecorator

Parameters:

NameTypeDescription
fields Requiredmongoose.IndexDefinitionAll Fields for this single index
optionsIndexOptions<T>Overwrite Schema Options, merged with original schema options

@index is used to set indices on the schema, this decorator acts like schema.index().

note

For Full-Text Search option weights all fields (from fields) have to also be defined in weights.

Example

@index({ article: 1, user: 1 }, { unique: true }) // compound index
@index({ location: '2dsphere' }) // single index with no options
@index({ article: 1 }, { partialFilterExpression: { stars: { $gte: 4.5 } } }) // single index with options
class Location {
@prop()
public article?: number;

@prop()
public user?: number;

@prop()
public stars?: number;

@prop({ type: Number, dim: 2 })
public location?: number[][];
}

Extra

Inheriting indexes from lower classes can be disabled with ModelOption disableLowerIndexes.