2 回答

TA貢獻1856條經(jīng)驗 獲得超11個贊
Batch 中的最大操作數(shù)為 500。如果您需要更多操作,則需要多個批處理。
沒有 API 可以確定 Batch 中的當前操作數(shù)。如果你需要它,你將不得不自己跟蹤它。

TA貢獻1796條經(jīng)驗 獲得超7個贊
我發(fā)現(xiàn)在使用 python 時處理 500 批限制的最佳方法是將我想要發(fā)送到 Firestore 的所有數(shù)據(jù)放在“平面”字典中,這樣我就可以處理每個唯一的文檔。該字典將每個文檔的鍵作為以下形式:“collection_document_collection_document...”,而該鍵的值將是具有以下內(nèi)容的字典:
{'action': 'set', 'reference': reference, 'document': {}}
'action' 可以是 'set'、'update' 或 'delete','reference' 鍵是實際的 Firestore 引用,而 'document' 只是文檔。例如,這是位于不同位置的 2 個文檔。
{
'user_data_roger':
{'action': 'set', 'reference': db.collection('user_data').document('roger'), 'document': {'name': 'Roger', 'age': 37}},
'user_data_roger_works_april':
{'action': 'update', 'reference': db.collection('user_data').document('roger').collection('works').document('april'), 'document': {'is_valid': True, 'in_progress': True, 'level':5}},
}
處理完我需要的所有數(shù)據(jù)后,我想將字典拆分為 500 個項目的數(shù)組,然后使用批處理的“操作”鍵將所有這些項目添加到批處理中。
# Convert dictionary to a list
dictionary_list = []
for item in dictionary:
dictionary_list.append(dictionary.get(item))
# Split List in lists containing 500 items per list
list_to_batch = [dictionary_list[item:item+500] for item in range(0, len(dictionary_list), 500)]
# Finally iterate through the 'list_to_batch' add each item to the batch and commit using a for loop
for item in list_to_batch:
batch = db.batch()
for document in item:
if document['action'] == 'set':
batch.set(document['reference'], document['value'])
elif draw['action'] == 'update':
batch.update(document['reference'], document['value'])
else:
batch.delete(document['reference'], document['value'])
# Finally commit the batch
batch.commit()
在我處理完我需要的所有數(shù)據(jù)后的特殊情況下,我最終進行了超過 700,000 次操作,因此請注意計費:-D
添加回答
舉報