-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove field AccountsDeclaredAsLevyPayers from Users collection
- Loading branch information
1 parent
72b7244
commit 0be2daa
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
src/Data/Recruit.ChangeScripts/010-users_removing_accountsDeclaredAsLevyPayers.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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."); | ||
} |