question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

types: typing error on `bulkWrite` with `AnyBulkWriteOperation<T>`

See original GitHub issue

Prerequisites

  • 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

  1. bulkWrite will show a typescript error if passed a variable of type AnyBulkWriteOperation<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:closed
  • Created a year ago
  • Reactions:1
  • Comments:8

github_iconTop GitHub Comments

1reaction
keyCatcommented, Aug 2, 2022

@vkarpov15 The issue is that AnyBulkWriteOperation treated AnyBulkWriteOperation<Document> for each and every model and unable to accept anything, except for AnyBulkWriteOperation<Document> or any, while it should be able to accept AnyBulkWriteOperation<TSchema> by default.

0reactions
vkarpov15commented, Aug 7, 2022

Should be fixed by #12167

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found