Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-5637

bulk write APIs do not use pkFactory when creating document _ids

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Bulk API
    • Labels:
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      What problem are you facing?

      `Collection.bulkWrite` and `Collection.initialize*BulkOp().execute()` do not respect the user provided pkFactory when generating document ids.

      This can lead to inconsistent data if regular crud APIs are used as well as bulk APIs.

      This notably does not apply to Collection.insertMany().
       

      What driver and relevant dependency versions are you using?

      latest, but the behavior has been here forever.

      Steps to reproduce?

      import { randomBytes } from 'crypto';
      
      import { MongoClient } from './src/mongo_client';
      
      async function main() {
        const client = await MongoClient.connect(process.env.MONGODB_URI!, {
          pkFactory: {
            count: 0,
            createPk: function () {
              return this.count++;
            }
          }
        });
      
        const collection = client.db('foo').collection(randomBytes(8).toString('utf-8'));
      
        await collection.insertMany([{ a: 1 }, { a: 2 }, { a: 3 }]);
      
        await collection.bulkWrite([{ insertOne: { a: 1 } }]);
      
        console.log(await collection.find().toArray());
      }
      
      main();
      
      // output:
      // [
      //  { _id: 0, a: 1 },
      //  { _id: 1, a: 2 },
      //  { _id: 2, a: 3 },
      //  { _id: new ObjectId("65036cbf71bcb5518605c620"), a: 1 }
      // ]
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            bailey.pearson@mongodb.com Bailey Pearson
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: