diff --git a/services/web/scripts/helpers/batchedUpdate.js b/services/web/scripts/helpers/batchedUpdate.js index 058cc3908f..1e3e3ca82f 100644 --- a/services/web/scripts/helpers/batchedUpdate.js +++ b/services/web/scripts/helpers/batchedUpdate.js @@ -7,13 +7,13 @@ if (process.env.BATCH_LAST_ID) { BATCH_LAST_ID = ObjectId(process.env.BATCH_LAST_ID) } -async function getNextBatch(collection, query, maxId, projection) { +async function getNextBatch(collection, query, maxId, projection, options) { maxId = maxId || BATCH_LAST_ID if (maxId) { query._id = { $gt: maxId } } const entries = await collection - .find(query, { readPreference: ReadPreference.SECONDARY }) + .find(query, options) .project(projection) .sort({ _id: 1 }) .limit(BATCH_SIZE) @@ -28,17 +28,31 @@ async function performUpdate(collection, nextBatch, update) { ) } -async function batchedUpdate(collectionName, query, update, projection) { +async function batchedUpdate( + collectionName, + query, + update, + projection, + options +) { await waitForDb() const collection = db[collectionName] + options = options || {} + options.readPreference = ReadPreference.SECONDARY + projection = projection || { _id: 1 } let nextBatch let updated = 0 let maxId while ( - (nextBatch = await getNextBatch(collection, query, maxId, projection)) - .length + (nextBatch = await getNextBatch( + collection, + query, + maxId, + projection, + options + )).length ) { maxId = nextBatch[nextBatch.length - 1]._id updated += nextBatch.length diff --git a/services/web/scripts/history/upgrade_v1_without_conversion_if_no_sl_history.js b/services/web/scripts/history/upgrade_v1_without_conversion_if_no_sl_history.js index 65250aa4fc..24a9b68b62 100644 --- a/services/web/scripts/history/upgrade_v1_without_conversion_if_no_sl_history.js +++ b/services/web/scripts/history/upgrade_v1_without_conversion_if_no_sl_history.js @@ -79,6 +79,9 @@ async function main() { _id: 1, overleaf: 1, } + const options = { + hint: { _id: 1 }, + } await batchedUpdate( 'projects', { @@ -88,7 +91,8 @@ async function main() { ], }, processBatch, - projection + projection, + options ) console.log('Final') console.log(RESULT)