-
Type: Task
-
Resolution: Works as Designed
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
Labels:
-
Environment:Ubuntu 16.04, mongodb@3.2.3, mongodb server 4.0.6
-
Empty show more show less
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!') } }