I'm using migrate-mongo
and get the following error when trying to run some migrations.
~$ npx migrate-mongo up
ERROR: Could not migrate up 20221201223533-add-indexes.js: Cannot create new
indexes on existing collection xxx.xxx in a multi-document transaction.
MongoServerError: Cannot create new indexes on existing collection xxx.xxx in
a multi-document transaction.
Googling the error comes up with zero results. Similar questions about "Cannot do xyz in a multi-document transaction" seem to suggest the collections need to exist before running the transaction. I've checked and double checked to make sure the collections exist, but still get the above error.
My code looks something like this:
exports.up = async (db, client) => {
const fooCollection = db.collection('foo')
const barCollection = db.collection('bar')
const session = client.startSession();
try {
await session.withTransaction(async () => {
await fooCollection.createIndex({ foo: 1 }, { session });
await barCollection.createIndex({ bar: 1 }, { session });
});
} finally {
await session.endSession();
}
}
exports.down = async (db, client) => { ... }
How can I create a new index within a transaction?