import sys import os curPath = os.path.abspath(os.path.dirname(__file__)) rootPath = os.path.split(curPath)[0] sys.path.append(rootPath) import common from common import configs as cfg from sync import utils prod_district_stat = common.get_mongodb_collection(cfg.MONGO_URI, "fangjia_stat", "district_stat") test_district_stat = common.get_mongodb_collection(cfg.TEST_URI, "fangjia_stat", "district_stat") for index in prod_district_stat.list_indexes(): keys = list() for key in index["key"]: keys.append((key, index["key"][key])) print(index) if "unique" in index: test_district_stat.create_index(keys, background=True, unique=index["unique"]) else: test_district_stat.create_index(keys, background=True) for seaweed in utils.get_all_test_seaweed(): sync_condition = { "city": seaweed["city"], "region": seaweed["region"], "district_name": seaweed["name"], "weekend": {"$gt": common.parse_date("2018-03-01")} } print(sync_condition) total = prod_district_stat.count(sync_condition) count = 0 for dbo in prod_district_stat.find(sync_condition, no_cursor_timeout=True).batch_size(1000): count += 1 test_district_stat.save(dbo) if count % 1000 == 0: print(str(count) + "/" + str(total))