Can't use string IDs with Typescript in MongoDB driver

58 Views Asked by At

I'm trying to update my node.js driver for MongoDb from 4.13 to 5.x or 6.x. I'm using TypeScript.

For historical reasons, I'm using strings instead of BSON ObjectIDs for my document IDs. When I upgrade, I get this error:

error TS2322: Type 'string' is not assignable to type 'Condition<ObjectId>'.

136     await this.collection.updateOne({ _id: this.id }, [
                                          ~~~

  node_modules/mongodb/mongodb.d.ts:7381:5
    7381     _id: InferIdType<TSchema>;
             ~~~
    The expected type comes from property '_id' which is declared here on type 'Filter<Document>'

If I cast the ID value to "any", it compiles.

Is there a supported way to use string IDs?

0

There are 0 best solutions below