Closed JaredReisinger closed 3 years ago
Wow... this was apparently just broken in commit https://github.com/Automattic/mongoose/commit/1c66cc972c90529bd0ffe8496e9de866b92a97fa ("use mongodb's index specification")
The author of #10538 probably intended to use mongodb driver's IndexSpecification introduced in v4.0.0 of the package (mongodb driver's codebase was rewritten in TypeScript):
But the problem is that mongoose v5.x still relies on older versions of mongodb and @types/mongodb
which has a different definition for IndexSpecification
:
Ahhh.... I bet you're right. That's quite a conundrum... Did mongo actually change their API for this (createIndex()
), or did they just tweak the type definitions?
I think they just tweaked the types.
Even changing the type to native mongdb, it's not creating the indexes.
Example:
Code:
Mongoose creating index log. Note that this is creating the index by another way than the field scoped indexes:
Not reflecting at mongodb:
For now, reverting to "mongoose": "5.13.5"
is working for me.
Do you want to request a feature or report a bug? bug (in Typescript types)
What is the current behavior? The definition of Schema.index() (at https://github.com/Automattic/mongoose/blob/master/index.d.ts#L1279-L1280) says that the
fields
argument is of typemongodb.IndexSpecification
, but mongoose actually takes its own shorthand syntax for the indexed fields, namely an object containing the indexed fields and the sort order (see https://mongoosejs.com/docs/api/schema.html#schema_Schema-index and https://github.com/Automattic/mongoose/blob/master/lib/schema.js#L1549-L1560).If the current behavior is a bug, please provide the steps to reproduce.
This is as easy as the following schema definition (as a
.ts
file):... you'll get an error on the
whatever: 1
in theschema.index()
call, stating:Here's the
tsconfig.json
file, though it shouldn't matter much:What is the expected behavior? No error, the
fields
argument toschema.index()
should probably have a type definition similar toSchemaDefinition<>
/DocumentDefinition<>
that leverages theSchemaDefinitionType
to define the allowable keys. Maybe something like(The
SchemaDefinition<>
type might need to be in that chain as well... it's not clear to me what specific value that layer adds.)What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version. Node.js: 14.16.0 mongoose: 5.13.6 mongodb: irrelevant