为什么Firestore中没有更新我的所有文档?

2024-04-25 00:16:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用python尝试向firestore集合中的750多个文档添加字段,但是当尝试这样做时,只有我的前55个文档得到更新。我假设这是因为firestore的写限制,但不知道原因。在

集合结构

enter image description here

注意:actividades是一个大小约为20个元素的数组,每个元素只有3个属性。在

enter image description here

代码

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

到底发生了什么?我该怎么解决?在


Tags: 文档importapp元素dbadmincountupdate
1条回答
网友
1楼 · 发布于 2024-04-25 00:16:19

不应在遍历集合时对其进行变异。取而代之的是获取文档引用,然后迭代。在

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

参考号:https://github.com/GoogleCloudPlatform/google-cloud-python/issues/6033

更新:Firestore python客户端似乎有一个20 second timeout for the generator returned from a query。在

相关问题 更多 >