-
Type: Bug
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: TypeScript
If an index signature is used when defining a document interface, an error will be reported when using addToSet in the bulkWrite method.
But if addToSet is used in the updateOne method, or addToSet in the bulkWrite method is used in an interface Without index signature, this will not happen. The former (the addToSet in the updateOne) additionally results in no restrictions on field types due to the index signature.
Code Snippet:
import type { Collection, Document } from 'mongodb'; interface TestDocument { readonly myId: number; readonly mySet: number[]; } const collection = undefined as unknown as Collection<TestDocument>; collection.updateOne({ myId: 0 }, { $addToSet: { mySet: 0 } }); collection.bulkWrite([ { updateOne: { filter: { myId: 0 }, update: { $addToSet: { mySet: 0 }, }, }, }, ]); interface IndexSingatureTestDocument extends Document { readonly myId: number; readonly mySet: number[]; } const indexSingatureCollection = undefined as unknown as Collection<IndexSingatureTestDocument>; indexSingatureCollection.updateOne({ myId: 0 }, { $addToSet: { mySet: '' } }); indexSingatureCollection.bulkWrite([ { updateOne: { filter: { myId: 0 }, update: { $addToSet: { mySet: 0 }, // }, }, }, ]);
My TypeScript version is 5.1.3.
I tried both >=6.1.0 and >=5.7.0 versions of mongodb, and it reproduced stably.
—
Acceptance Criteria
- The code sample from the ticket description should not show errors for:
- Collection.updateOne()
- Collection.updateMany()
- bulkWrite.updateOne
- bulkWrite.updateMany
- Change the type of `update` parameter to be `UpdateFilter<TSchema> | Document[]`
- Cover those cases with type tests
- The code sample from the NODE-4664 should not show errors for the same methods listed above.
- Fields of any type should not show errors.
- Add API comments explaining that:
- `UpdateFilter<TSchema>` covers a document that contains update operator expressions
- `Document[]` covers aggregation pipelines in the update filter
- Verify if documentation needs to be updated.
- duplicates
-
NODE-4664 TypeScript compilation fails when calling bulkWrite with $addToSet on a field that is specified in the filter and TSchema = any
- Backlog