【问题标题】:how to delete documents in batch in firestore using python如何使用python在firestore中批量删除文档
【发布时间】:2020-11-06 12:46:29
【问题描述】:

我的 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'

寻求帮助!

【问题讨论】:

  • 您必须使用get() 实际执行查询并从结果中获取文档引用才能将它们传递给batch.delete()。您不能传递 Query 对象。
  • 不,没用。
  • 如果您需要这方面的帮助,您应该展示您尝试过的方法,并说明没有按您预期的方式工作。
  • 请编辑问题以显示更新后的代码。不要在评论中留下它。使用问题底部的编辑链接。
  • 已编辑。您可以查看我尝试过的完整 sn-p。

标签: python firebase google-cloud-firestore


【解决方案1】:

您将 DocumentSnapshot 对象传递给 batch.delete(),这是不允许的。您必须改为传递 DocumentReference 对象,该对象可以在 DocumentSnapshot 的属性中找到。

  batch.delete(doc.reference)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    • 2021-02-13
    相关资源
    最近更新 更多