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

pkFactory is only applied to insert operations, not upsert nor bulk

    • Type: Icon: Bug Bug
    • Resolution: Gone away
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.3.3
    • Component/s: None
    • Labels:
    • Environment:
      node: v8.16.1
      npm: 6.11.3

      the pkFactory that is being used to use a custom id generator, is being applied to insert statements. But not for upsert statements or bulk insert statements.

      Reproduction:

      const Client = require('mongodb');
      
      function CustomPKFactory() {}
      CustomPKFactory.prototype = new Object();
      CustomPKFactory.count = 0;
      CustomPKFactory.createPk = function() {
        return ++CustomPKFactory.count;
      };
      
      const host = 'mongodb://localhost:27017';
      const options = { pkFactory: CustomPKFactory };
      
      Client.connect(host, options, async (err, client) => {
        const db = client.db('pkFactory');
        const col = db.collection('test_custom_key');
        await col.deleteMany({});
      
        // ✓ expected _id: 1 | received _id: 1
        await col.insertOne({ a: 1 });
      
        // ⨉ expected _id: 2 | received _id: ObjectId(…)
        await col.findOneAndUpdate({ a: 2 }, { $set: { b: 2 } }, { upsert: true });
      
        const bulk = col.initializeUnorderedBulkOp();
      
        // ⨉ expected _id: 3 | received _id: ObjectId(…)
        bulk.insert({ a: 3 });
      
        // ⨉ expected _id: 4 | received _id: ObjectId(…)
        bulk
          .find({ a: 4 })
          .upsert()
          .update({ $set: { b: 4 } });
      
        await bulk.execute();
      });
      

      Requested output of db.isMaster()

      db.isMaster()
      {
          "hosts" : [ 
              "127.0.0.1:3001"
          ],
          "setName" : "pkFactory",
          "setVersion" : 9516979,
          "ismaster" : true,
          "secondary" : false,
          "primary" : "127.0.0.1:27017",
          "me" : "127.0.0.1:27017",
          "electionId" : ObjectId("7fffffff00000000000000a4"),
          "lastWrite" : {
              "opTime" : {
                  "ts" : Timestamp(1572097963, 1),
                  "t" : NumberLong(164)
              },
              "lastWriteDate" : ISODate("2019-10-26T13:52:43.000Z"),
              "majorityOpTime" : {
                  "ts" : Timestamp(1572097963, 1),
                  "t" : NumberLong(164)
              },
              "majorityWriteDate" : ISODate("2019-10-26T13:52:43.000Z")
          },
          "maxBsonObjectSize" : 16777216,
          "maxMessageSizeBytes" : 48000000,
          "maxWriteBatchSize" : 100000,
          "localTime" : ISODate("2019-10-26T13:52:44.010Z"),
          "logicalSessionTimeoutMinutes" : 30,
          "minWireVersion" : 0,
          "maxWireVersion" : 7,
          "readOnly" : false,
          "ok" : 1.0,
          "operationTime" : Timestamp(1572097963, 1)
      }
      

       

       

            Assignee:
            daniel.aprahamian@mongodb.com Daniel Aprahamian (Inactive)
            Reporter:
            stephan.meijer@gmail.com Stephan Meijer
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: