![](/img/trans.png)
[英]How to update a field in a Firestore document only if it exists (using Python)?
[英]How to use firestore field incremental using python?
我正在寻找一种方法来增加和更新 Python 中的 firestore 字段。
似乎在最近的 google-cloud-firestore 1.2.0 中添加了增量功能。 问题7533 ? 这如何在 python 中实现。 任何示例实现参考代码都会非常有帮助。
from google.cloud import firestore
......
upd_ref_path.update({u'test_col': firestore.FieldValue.increment(1)})
我收到以下错误消息错误:AttributeError: module 'google.cloud.firestore' has no attribute 'FieldValue'
更新 -
如果来自 firestore 模块,此功能需要附魔才能使用。 问题 - 问题 8173 。 为此开放。
对于临时解决方案,使用如下:
from google.cloud.firestore_v1 import Increment
doc_ref.set({u'test_col': Increment(1)}, merge=True)
查看文档 。 您应该可以像以下一样使用它:
from google.cloud import firestore
......
upd_ref_path.update({u'test_col': firestore.transforms.Increment(1)})
希望有所帮助
如果您从firebase_admin
导入它应该是这样的
washington_ref = db.collection(u'cities').document(u'DC')
washington_ref.update({"population": firestore.Increment(50)})
检查文档以获取更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.