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

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

      Minimal example:

      Unable to find source-code formatter for language: node. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      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

      Unable to find source-code formatter for language: node. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      [{
      	_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: