mongoose setting schema index throws exception after version update, Property 'key' is missing but required in type 'IndexSpecification'

Viewed 134

I used to set a schema index using this syntax:

mySchema.index({ name: 'text' });

So name is a property of my schema.

After mongoose update, I'm getting the error below:

Argument of type '{ name: string; }' is not assignable to parameter of type 'IndexSpecification'.
  Property 'key' is missing in type '{ name: string; }' but required in type 'IndexSpecification'.ts(2345)

What key should I set here?

mongoose versions:

mongoose@^5.13.6

@types/mongoose@^5.11.97

Thanks!

1 Answers

Following mongodb documentation,

key:

Specifies the index’s fields. For each field, specify a key-value pair in which the key is the name of the field to index and the value is either the index direction or index type. If specifying direction, specify 1 for ascending or -1 for descending.

So the answer would be:

mySchema.index({ key: { name: 1 }, name: 'nameIndex' });

Related