使用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

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM