From 0be2daa456b35d04a68b30f18416fcc382901752 Mon Sep 17 00:00:00 2001 From: vas Date: Fri, 17 Jan 2020 12:29:10 +0000 Subject: [PATCH] Remove field AccountsDeclaredAsLevyPayers from Users collection --- ...s_removing_accountsDeclaredAsLevyPayers.js | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/Data/Recruit.ChangeScripts/010-users_removing_accountsDeclaredAsLevyPayers.js diff --git a/src/Data/Recruit.ChangeScripts/010-users_removing_accountsDeclaredAsLevyPayers.js b/src/Data/Recruit.ChangeScripts/010-users_removing_accountsDeclaredAsLevyPayers.js new file mode 100644 index 0000000000..d0c44ba67d --- /dev/null +++ b/src/Data/Recruit.ChangeScripts/010-users_removing_accountsDeclaredAsLevyPayers.js @@ -0,0 +1,55 @@ +{ + print("Start removing accountsDeclaredAsLevyPayers from users."); + + const query = { + "accountsDeclaredAsLevyPayers": { $exists: true } + }, + batchUpdateLimit = 500, + coll = db.users; + let passThrough = 1, + maxLoops = Math.ceil(coll.find().count(query) / batchUpdateLimit); + + if (maxLoops === 0) { + maxLoops = 1; + } + + print(query); + + do { + let matchedDocs = coll.aggregate([ + { + $match: query + }, + { + $sort: { "lastUpdated": 1 } + }, + { + $limit: batchUpdateLimit + } + ]); + + print(`Found ${matchedDocs._batch.length} document(s) to operate on in pass-through ${passThrough} of ${maxLoops}.`); + + while (matchedDocs.hasNext()) { + let doc = matchedDocs.next(); + + let writeResult = coll.update({ + "_id": doc._id + }, { + $unset: { "accountsDeclaredAsLevyPayers": "" } + }); + + if (writeResult.hasWriteConcernError()) { + printjson(writeResult.writeConcernError); + quit(14); + } + + print(`Updated document '${doc._id}', removed accountsDeclaredAsLevyPayers.`); + } + + passThrough++; + } + while (passThrough <= maxLoops && coll.find().count(query) > 0); + + print("Finished removing accountsDeclaredAsLevyPayers from users."); +} \ No newline at end of file