Skip to content

Instantly share code, notes, and snippets.

@Laisky
Last active August 29, 2015 14:06
Show Gist options
  • Save Laisky/1574e8e73b39d659be62 to your computer and use it in GitHub Desktop.
Save Laisky/1574e8e73b39d659be62 to your computer and use it in GitHub Desktop.
replace duanwei with 儿童
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import pymongo
NEW_HASH = '0f533b43e264260e17fed6a15e05ffd6c3f8e3b1'
HASH_TEXT = 'duanwei-儿童'
OLD_HASH_LS = ['efacd9afc28bed0712c4e904585f10a277fbfc7d',
'59f420d90a824716eb07df9b698aa2ecedf3f0dc',
'558cddba6639b9de3c5dbb0cc5953b0c485ca349',
'51ba3c692aca1c42b5fbf9860325450c226a13ea']
log = logging.getLogger(__name__)
def run():
conn = pymongo.Connection(host='192.168.1.202', port=27017, safe=True)
# conn = pymongo.Connection(safe=True)
# db = conn['tss-clean-data-0920']
db = conn['tss-dev']
subjects = db['subjects']
layers = db['layers']
metadata = db['metadata']
# metadata
log.info('metadata')
meta_docus = metadata.find(
{
'subjectFeatures.' + OLD_HASH_LS[0]: {'$exists': 1}
}
)
for docu in meta_docus:
for feature in OLD_HASH_LS:
if feature in docu['subjectFeatures']:
del docu['subjectFeatures'][feature]
if feature in docu['booleanSubjectFeaturesGroups']['duanwei']:
docu['booleanSubjectFeaturesGroups']['duanwei'].remove(feature)
docu['subjectFeatures'][NEW_HASH] = HASH_TEXT
if NEW_HASH not in docu['booleanSubjectFeaturesGroups']['duanwei']:
docu['booleanSubjectFeaturesGroups']['duanwei'].append(NEW_HASH)
del docu['_id']
metadata.update(
{'name': docu['name']},
{'$set': docu},
multi=True
)
# subjects
log.info('subjects')
sbj_docus = subjects.find(
{
'subjectFeatures.' + OLD_HASH_LS[0]: {'$exists': 1}
}
)
for docu in sbj_docus:
sum_val = 0
for feature in OLD_HASH_LS:
if feature in docu['subjectFeatures']:
sum_val += docu['subjectFeatures'][feature]
del docu['subjectFeatures'][feature]
del docu['_id']
docu['subjectFeatures'][NEW_HASH] = sum_val
subjects.update(
{'set': docu['set'], 'layer': docu['layer'], 'sid': docu['sid']},
{'$set': docu},
multi=True
)
# layers
log.info('layers')
layer_docus = layers.find(
{'subjectFeatures': {'$in': OLD_HASH_LS}}
)
for docu in layer_docus:
for feature in OLD_HASH_LS:
if feature in docu['subjectFeatures']:
docu['subjectFeatures'].remove(feature)
if NEW_HASH not in docu['subjectFeatures']:
docu['subjectFeatures'].append(NEW_HASH)
del docu['_id']
layers.update(
{'set': docu['set'], 'layer': docu['layer']},
{'$set': docu},
multi=True
)
if __name__ == '__main__':
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment