types: typing error on `bulkWrite` with `AnyBulkWriteOperation<T>`
See original GitHub issuePrerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Mongoose version
6.4.0
Node.js version
16
MongoDB server version
5
Description
bulkWrite
will show a typescript error if passed a variable of typeAnyBulkWriteOperation<ISchema>[]
Steps to Reproduce
function modelBulkTest() {
interface ISchema {
_id: string;
num: number;
}
const schema = new Schema({
_id: { type: String },
num: Number
});
const M = model<ISchema>('Test', schema);
const ops: AnyBulkWriteOperation<ISchema>[] = [
{
updateOne: {
filter: { _id: 'test' },
update: {
$inc: { num: 1 }
},
upsert: true
}
}
];
M.bulkWrite(ops);
}
const ops: AnyBulkWriteOperation<ISchema>[]
Argument of type 'AnyBulkWriteOperation<ISchema>[]' is not assignable to parameter of type 'AnyBulkWriteOperation<Document>[]'.
Type 'AnyBulkWriteOperation<ISchema>' is not assignable to type 'AnyBulkWriteOperation<Document>'.
Type '{ insertOne: InsertOneModel<ISchema>; }' is not assignable to type 'AnyBulkWriteOperation<Document>'.
Type '{ insertOne: InsertOneModel<ISchema>; }' is not assignable to type '{ insertOne: InsertOneModel<Document>; }'.
The types of 'insertOne.document' are incompatible between these types.
Type 'OptionalId<ISchema>' is not assignable to type 'OptionalId<Document>'.
Type 'OptionalId<ISchema>' is not assignable to type '{ _id?: ObjectId | undefined; }'.
Types of property '_id' are incompatible.
Type 'string | undefined' is not assignable to type 'ObjectId | undefined'.
Type 'string' is not assignable to type 'ObjectId'.
Expected Behavior
The test should pass.
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:8
Top Results From Across the Web
db.collection.bulkWrite() — MongoDB Manual
bulkWrite () throws a BulkWriteError exception on errors (unless the operation is part of a transaction on MongoDB 4.0). See Error Handling inside...
Read more >Mongodb bulk write error - pymongo - Stack Overflow
from pymongo.errors import BulkWriteError try: bulk.execute() except ... to manage custom types, In my case I was trying to pass a hash type...
Read more >Bulk write error returns incorrect index in WriteError for ...
When performing a bulk write, if there is a BulkWriteError thrown (this is for MongoDB 4.2 and above), the index of the "WriteError"...
Read more >A guide to bulk write operations in MongoDB with C# - DEV ...
We set our User model as a generic type parameter to the WriteModel ... If an error occurs of one of the insert...
Read more >MongoDB bulkWrite() - Database.Guide
bulkWrite () method performs multiple write operations with controls for order of execution. Bulk write operations affect a single collection ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@vkarpov15 The issue is that
AnyBulkWriteOperation
treatedAnyBulkWriteOperation<Document>
for each and every model and unable to accept anything, except forAnyBulkWriteOperation<Document>
orany
, while it should be able to acceptAnyBulkWriteOperation<TSchema>
by default.Should be fixed by #12167