我正在嘗試使用bulk_write指令插入大量文檔(+1M)。為此,我創建了一個 InsertOne 函數列表。python version = 3.7.4pymongo version = 3.8.0文檔創建:document = { 'dictionary': ObjectId(dictionary_id), 'price': price, 'source': source, 'promo': promo, 'date': now_utc, 'updatedAt': now_utc, 'createdAt:': now_utc }# add line to debugif '_id' in document.keys(): print(document)return document我通過從元素列表中添加新字段來創建完整的文檔列表,并使用 InsertOne 創建查詢bulk = []for element in list_elements: for document in documents: document['new_field'] = element # add line to debug if '_id' in document.keys(): print(document) insert = InsertOne(document) bulk.append(insert)return bulkbulk_write我使用命令進行插入collection.bulk_write(bulk, ordered=False)我附上文檔https://api.mongodb.com/python/current/api/pymongo/collection.html#pymongo.collection.Collection.bulk_write根據文檔,該_id字段是自動添加的 Parameter - document: The document to insert. If the document is missing an _id field one will be added.不知何故,這似乎是錯誤的,因為其中一些具有相同的價值。對于 1M 文檔中的 700k 收到此錯誤(當然有不同的 _id)對 'E11000 duplicate key error collection: database.collection index: _id_ dup key: { _id: ObjectId(\'5f5fccb4b6f2a4ede9f6df62\') }' 我來說似乎是 pymongo 的錯誤,因為我在很多情況下使用了這種方法,但我沒有使用如此大小的文檔該_id字段肯定必須是唯一的,但是,由于這是由 pymongo 自動完成的,我不知道如何解決這個問題,也許使用 UpdateOne 和 upsert True 以及不可能的過濾器并希望得到最好的結果。我將不勝感激任何解決方案或解決這個問題
添加回答
舉報
0/150
提交
取消