Rocketchat fails after upgrade with index error

We're running rocketchat and are upgrading daily on an automated basis with allowing even major version upgrades. Today we've received the upgrade to 5.0 and run into two errors.

Obviously we're running rocketchat within containers (actually this system is still running on a docker basis).

MongoDB connectivity

Until now we've been running mongodb 4.2. With the current release of rocketchat, the connection strings slightly changed, which is luckily annotated by the devs in the release notes.

You can spot that you're missing the configuration when the rocketchat logs show that the connection to the database has been closed unexpectedly.

The important part is to append the query parameter directConnection=true to the connection string. The configurations might look like this:

MONGO_URL=mongodb://db:27017/rocketchat?replicaSet=rs0&directConnection=true
MONGO_OPLOG_URL=mongodb://db:27017/local?replicaSet=rs0&directConnection=true
connection strings

Upgrade MongoDB

Working on the system is a good starting point to upgrade mongoDB to a current release. Just be aware that you need to upgrade in supported version steps like 4.0 -> 4.2 -> 4.4 -> 5.0.

MongoDB indices

Another error you may see is that rocketchat fails on altering indices after upgrade.

/app/bundle/programs/server/node_modules/fibers/future.js:313
                                                throw(ex);
                                                ^
errorClass [Error]: [An error occurred when creating an index for collection "users: An equivalent index already exists with the same name but different options. Requested index: { v: 2, unique: true, key: { username: 1 }, name: "username_1", sparse: true }, existing index: { v: 2, unique: true, key: { username: 1 }, name: "username_1", ns: "rocketchat.users", sparse: 1 }]
    at Collection.createIndex (packages/mongo/collection.js:770:15)
    at setupUsersCollection (packages/accounts-base/accounts_server.js:1812:9)
    at new AccountsServer (packages/accounts-base/accounts_server.js:74:5)
    at packages/accounts-base/server_main.js:7:12
    at module (packages/accounts-base/server_main.js:19:1)
    at fileEvaluate (packages/modules-runtime.js:336:7)
    at Module.require (packages/modules-runtime.js:238:14)
    at require (packages/modules-runtime.js:258:21)
    at /app/bundle/programs/server/packages/accounts-base.js:2395:15
    at /app/bundle/programs/server/packages/accounts-base.js:2402:3
    at /app/bundle/programs/server/boot.js:401:38
    at Array.forEach (<anonymous>)
    at /app/bundle/programs/server/boot.js:226:21
    at /app/bundle/programs/server/boot.js:464:7
    at Function.run (/app/bundle/programs/server/profile.js:280:14)
    at /app/bundle/programs/server/boot.js:463:13 {
  isClientSafe: true,
  error: 'An error occurred when creating an index for collection "users: An equivalent index already exists with the same name but different options. Requested index: { v: 2, unique: true, key: { username: 1 }, name: "username_1", sparse: true }, existing index: { v: 2, unique: true, key: { username: 1 }, name: "username_1", ns: "rocketchat.users", sparse: 1 }',
  reason: undefined,
  details: undefined,
  errorType: 'Meteor.Error'
}
error log from rocketchat

The solution here is to remove indices from the database and let rocketchat create them.

In my case, I needed to remove the following indices:

use rocketchat
db.users.dropIndexes()
db.rocketchat_message.dropIndexes()
db.rocketchat_room.dropIndexes()
remove problematic indices

Having done this, rocketchat started again without any issue.