12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- # -*- coding: utf-8 -*-
- # !/usr/bin/env python
- import datetime
- import os
- import sys
- import time
- os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'configs.production')
- PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..')
- sys.path.insert(0, PROJECT_ROOT)
- from script.base import init_env, get_logger
- init_env(interactive=False)
- logger = get_logger(__name__)
- from apilib.utils_mongo import BulkHandlerEx
- from apps.web.user.models import UserVirtualCard
- def remove_documents():
- _cls = UserVirtualCard
- dealerId = '629c0612003048029f9708fb'
- start_time = int(time.time())
- while True:
- now_time = int(time.time())
- if now_time - start_time > 2 * 60 * 60:
- logger.debug('time is over. wait next.')
- return
- items = _cls.get_collection().find(
- {'dealerId': dealerId}, {'_id': 1, 'openIds': 1}).limit(2000)
- bulker = BulkHandlerEx(_cls.get_collection())
- for item in items:
- if 'openIds' not in item or len(item['openIds']) == 0:
- # print(item)
- bulker.delete(query_dict={'_id': item['_id']})
- count = len(bulker.requests)
- if count > 0:
- logger.debug('prepare to delete {} rows.'.format(count))
- bulker.execute()
- bulker = BulkHandlerEx(_cls.get_collection()) # type: BulkHandlerEx
- if count < 2000:
- break
- logger.debug('delete all over.')
- remove_documents()
|