diff --git a/app/models/server/models/Sessions.js b/app/models/server/models/Sessions.js index 162840fac4b..fc84fb8e06f 100644 --- a/app/models/server/models/Sessions.js +++ b/app/models/server/models/Sessions.js @@ -434,6 +434,7 @@ export class Sessions extends Base { this.tryEnsureIndex({ instanceId: 1, sessionId: 1, userId: 1 }); this.tryEnsureIndex({ instanceId: 1, sessionId: 1 }); this.tryEnsureIndex({ sessionId: 1 }); + this.tryEnsureIndex({ userId: 1 }); this.tryEnsureIndex({ year: 1, month: 1, day: 1, type: 1 }); this.tryEnsureIndex({ type: 1 }); this.tryEnsureIndex({ ip: 1, loginAt: 1 }); diff --git a/server/startup/migrations/v211.js b/server/startup/migrations/v211.js index 4882731d6e8..1b0c93f4043 100644 --- a/server/startup/migrations/v211.js +++ b/server/startup/migrations/v211.js @@ -28,8 +28,21 @@ async function migrateSessions() { $match: { 'user.roles.0': { $exists: 1 } }, }]); + let actions = []; for await (const session of cursor) { - await Sessions.col.updateMany({ userId: session._id }, { $set: { mostImportantRole: getMostImportantRole(session.user.roles) } }); + actions.push({ + updateMany: { + filter: { userId: session._id }, + update: { $set: { mostImportantRole: getMostImportantRole(session.user.roles) } }, + }, + }); + if (actions.length === 100) { + await Sessions.col.bulkWrite(actions, { ordered: false }); + actions = []; + } + } + if (actions.length) { + await Sessions.col.bulkWrite(actions, { ordered: false }); } }