Last active
December 2, 2021 22:59
-
-
Save MrCreosote/9c9728669bdfadaa6cbfc501790be5e5 to your computer and use it in GitHub Desktop.
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
import os | |
import sys | |
from pymongo.mongo_client import MongoClient | |
WS_MONGO_PWD = 'WS_MONGO_PWD' | |
MONGO_COL_WSOBJS = 'workspaceObjVersions' | |
def get_ws_obj_collection(argv): | |
_, host, db, user = argv | |
pwd = os.environ[WS_MONGO_PWD] | |
client = MongoClient(host, authSource=db, username=user, password=pwd) | |
client.admin.command('ismaster') # check connection | |
return client[db][MONGO_COL_WSOBJS] | |
def main(): | |
dry_run = len(sys.argv) < 5 | |
if dry_run: | |
print("Dry run - not making any modifications. Add a 4th argument to the command " + | |
"line to turn off dry run mode") | |
col = get_ws_obj_collection(sys.argv[:4]) | |
# bad idea usually, but we know this is < 10K objects for this case | |
objs = [] | |
count = 0 | |
for o in col.find(): | |
meta = o.get('meta') | |
if meta: | |
for kv in meta: | |
if ( | |
kv['k'] is not None | |
and kv['v'] is not None | |
and len(kv['k']) + len(kv['v']) > 900 | |
): | |
objs.append((o, kv)) | |
count += 1 | |
if count % 1000 == 0: | |
print(count, file=sys.stderr) | |
print(f"Updating {len(objs)} meta keys") | |
count = 0 | |
for o, kv in objs: | |
if not dry_run: | |
col.update_one( | |
{'ws': o["ws"], 'id': o['id'], 'ver': o['ver']}, | |
{'$pull': {'meta': {'k': kv['k'], 'v': kv['v']}}}) | |
print(f'Removed key {kv["k"]} from {o["ws"]}/{o["id"]}/{o["ver"]}') | |
count += 1 | |
if count % 100 == 0: | |
print(count, file=sys.stderr) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment