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

`createIndex()` always fails when using a transaction

    • Type: Icon: Task Task
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
    • Environment:
      Ubuntu 16.04, mongodb@3.2.3, mongodb server 4.0.6

      When connected to a replica set started using https://www.npmjs.com/package/run-rs , the below script always errors out with "Cannot write to system collection test.system.indexes within a transaction." Does this mean it is impossible to create an index within a transaction?

       

      const mongodb = require('mongodb');
      
      run().catch(error => console.log(error));
      
      async function run() {
        const client = await mongodb.MongoClient.connect('mongodb://localhost:27017,localhost:27018,localhost:27019/test?replicaSet=rs', {
          useNewUrlParser: true
        });
        const db = client.db();  await db.dropDatabase();
        await db.createCollection('Test');  const session = await client.startSession();
        let success = false;
        for (let i = 0; i < 10; ++i) {
          try {
            session.startTransaction();
            await db.collection('Test').insertOne({ value: 'foo' }, { session });
            await db.collection('Test').createIndex({ value: 1 }, { session });
            await session.commitTransaction();
            success = true;
            break;
          } catch (err) {
            await session.abortTransaction();
            if (err.errorLabels != null && err.errorLabels.includes('TransientTransactionError')) { 
              console.log('Retry...');
              await new Promise(resolve => setTimeout(resolve, 100));
              continue;
            }
            throw err;
          }
        }  if (!success) { 
          throw new Error('Failed!')
        }
      }
       

            Assignee:
            matt.broadstone@mongodb.com Matt Broadstone
            Reporter:
            val@karpov.io Valeri Karpov
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: