python - 如何使用python在firestore中批量删除文档
问题描述
我的 Firestore 中有一个名为 XYZ 的集合。其中有 500 个不同字段的文档。我必须使用集合中的 where 子句删除多个文档。
cred = credentials.Certificate('XXXX')
app = firebase_admin.initialize_app(cred)
db = firestore.Client()
batch = db.batch()
doc_ref = db.collection('collection_name').where(u'month', '==', 07).get()
for doc in doc_ref:
batch.delete(doc)
batch.commit()
我试过了,但最终出现错误
AttributeError: AttributeError: 'DocumentSnapshot' object has no attribute '_document_path'
寻求帮助!
解决方案
您将 DocumentSnapshot 对象传递给batch.delete()
,这是不允许的。您必须改为传递 DocumentReference 对象,该对象可以在 DocumentSnapshot 的属性中找到。
batch.delete(doc.reference)
推荐阅读
- python - 如何在 fabric 2 中使用线程和 sudo?
- css - 如何使子元素相对于父元素的较小尺寸?
- facebook - 如何使用 facebook API 发布到我自己的页面
- date - 传输最新日期的文件名并删除较早日期的重复文件
- angular - 如何使用 Angular 6+ 表单生成器验证器?
- sql - SQL presto过滤日期列表?
- javascript - 如果存在则加载 json 并附加数据,而不是重新创建和覆盖 json 文件
- pyspark - 计算 pyspark 数据框列上的百分位数,忽略缺失值
- rust - 是否有理由在 Substrate 中对特定类型使用通用特征?
- c# - 使用 Get() 的简单 ping 控制器的属性路由不起作用