使用mongo进行去重操作

from pymongo import MongoClient

class Save(object):
    def __init__(self, host):
        self.client = MongoClient(host=host, port=27017)
        # 使用的mongo的数据表
        self.db = self.client.ImageSet

    def _save_data_mongodb(self, collect_name, data):
        # 定义一个去重的图标集名称
        self.collect_name = self.db[collect_name]
        history_record = self.collect_name.find_one({"_id": data['title_id']})
        if history_record:
            # 数据库中已经存在数据
            return False
        else:
            # 数据库中不存在,插入数据 
            self.collect_name.update_one({'_id': data['title_id']}, {'$set': data}, upsert=True)
            return True

  

posted @ 2019-07-16 14:09  不可描述的细节  阅读(1108)  评论(0编辑  收藏  举报