如題,我現(xiàn)在實現(xiàn)的方法是用語句來查詢:
result = db.collection.find_one(xxxx) if result: 更新數(shù)據(jù) else: 插入數(shù)據(jù)
但是,這種方法效率太低了,不知道有沒有更好的方法?
歡迎選擇我的課程,讓我們一起見證您的進步~~
MongoDB introduces a special update called upsert
. If no document meets the update conditions, a new document will be created based on this condition and the updated document; if a matching document is found, it will be updated normally.
See MongoDB’s documentation for details.
upsert:
db.collection.update(xx, xx,true)
Just set the third parameter to true
I don’t know if anyone has tested mongodb’s find_one. There is no problem when querying billions of data (tested).