我建立MongoDB數據庫中存在記錄,問題是,我想避免重複的條目。目前我(只檢查,如果條目不存在後插入文檔)這樣做:檢查是否MongoDB中
from pymongo import Connection
import pandas as pd
from time import strftime
from collections import OrderedDict
connection = Connection()
db = connection.mydb
collection = db.mycollection
data = pd.read_csv("data/myfile.csv", parse_dates=[2,5])
for i in range(len(data)):
if(collection.find({ "id": data.ix[0], \
"date1": data.ix[i, 2].strftime("%Y-%m-%d"), \
"date2": data.ix[i, 5].strftime("%Y-%m-%d"), \
"number": int(data.ix[i, 6]), \
"type": data.ix[i, 7]}).count() == 0):
collection.insert(here goes what I'd like to insert)
這確實工作得很好,但是這已經有顯著的性能問題(只〜100MB的數據),像做find()
似乎每次都會顯着減慢速度。
有沒有辦法加快速度?也許我從根本上做錯了? 我需要避免只在一組特定領域的重複,不是所有的人(即,也有「號碼2」,它可以是不同的,但我還是想把它作爲複製如果所有其他領域相匹配)。
你不必有什麼東西是一個主鍵,以在其上具有唯一索引! –