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