【问题标题】:Why aren't all my documents updated in Firestore?为什么我的所有文档都没有在 Firestore 中更新?
【发布时间】:2019-02-20 22:44:02
【问题描述】:

我正在使用 python 尝试将字段添加到 firestore 集合中的 750 多个文档,但是在尝试执行此操作时,只有我的前 55 个文档得到更新。我假设这是由于 Firestore 写入限制而发生的,但我不明白为什么。

收藏结构

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

代码

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

究竟发生了什么,我该如何解决?

【问题讨论】:

  • 您认为遇到了哪个 Firestore documented limits?您是否建议执行更新时计数不同,而不是像现在显示的那样将其注释掉?
  • 不一样。没有 cmets 的计数是 757。我真的不知道为什么会发生,但我认为这是因为 Firestore 限制之一。

标签: python firebase google-cloud-firestore firebase-admin


【解决方案1】:

您不应该在迭代集合时对其进行变异。相反,获取文档引用然后迭代。

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

【讨论】:

  • 为什么在迭代时改变集合不好?
  • 一般的好习惯是不要改变你正在迭代的东西,因为它会导致意想不到的结果。经典示例是 with a list,但它也适用于此处。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-06-27
  • 2018-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-29
  • 2020-10-03
相关资源
最近更新 更多