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

insertedIds don't correspond to initial operation ordering in an unordered bulkwrite

    • 2
    • 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?

      When a bulkWrite is unordered, the indexes in the insertedIds do not correspond to the ordering of operation.

      Minimal example:

      const mongodb = require('mongodb');
      
      const unorderedBulkWrite = async () => {
      	const client = new mongodb.MongoClient('mongodb://localhost:26000/?replicaSet=replset');
      	await client.connect();
      
      	const collection = client.db('my_db').collection('test');
      	// create some data
      	await collection.bulkWrite([
      		{ insertOne: { _id: 1, a: 2 } },
      		{ insertOne: { _id: 2, a: 3 } },
      	]);
      	// unordered bulkWrite
      	const reply = await collection.bulkWrite([
      		{ updateOne: { filter: { _id: 1 }, update: { $set: { a: 0 } } } },
      		{ insertOne: { _a: 0 } },
      		{ updateOne: { filter: { _id: 2 }, update: { $set: { a: 0 } } } },
      		{ insertOne: { a: 0 } },
      	], { ordered: false });
      	console.log(reply.result.insertedIds);
      	return reply.result.insertedIds;
      
      }
      
      unorderedBulkWrite();
      

      Then the insertedIds field looks like

      [{
      	_id: '63b2efb47fb05e8aa2a06173',
      	index: 0
      }, {
      	_id:'63b2efb47fb05e8aa2a06174', 
      	index:1
      }]
      

      The indexes (0 and 1) don't match the indexes of the insert operations in the initial ordering. It should be 1 and 3.

      This bug is similar with this (old, now fixed) bug https://github.com/mongodb/node-mongodb-native/commit/3be7f86a2dc6f7861d402b451446fc2a529007a6, so the solution might be similar.

            Assignee:
            bailey.pearson@mongodb.com Bailey Pearson
            Reporter:
            lucas.mercier@bruce.work Lucas Mercier
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: