Why aren't all my documents updated in Firestore?

对着背影说爱祢 提交于 2019-12-02 17:43:32

问题


I'm using python for trying to add a field to more than 750 documents in a firestore collection, but when trying to do this, only my first 55 documents get updated. I'm assuming this happens because of the firestore write limits but don't really get why.

COLLECTION STRUCTURE

Note: actividades is an array with a size around of 20 elements and each element has only 3 properties.

CODE

import firebase_admin
from firebase_admin import credentials, firestore

cred = credentials.Certificate('avancesAccountKey.json')
default_app = firebase_admin.initialize_app(cred)
db = firestore.client()

count = 0
avances = db.collection('avances').get()
for e in avances:  
    db.collection('avances').document(e.id).update({
        'vigente': False
    })
    count += 1

print(count) # count is 55 when trying to update, 757 otherwise

What is exactly happening and how I can solve this?


回答1:


You shouldn't mutate the collection as you iterate through it. Instead, fetch the document references and then iterate.

count = 0
documents = [snapshot.reference for snapshot in db.collection('avances').get()]
for document in documents:
    document.update({u'vigente': False})
    count += 1

Reference: https://github.com/GoogleCloudPlatform/google-cloud-python/issues/6033

Update: It appears the Firestore python client does have a 20 second timeout for the generator returned from a query.



来源:https://stackoverflow.com/questions/52360438/why-arent-all-my-documents-updated-in-firestore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!