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