如何使用python在firestore中批量删除文档

ast*_*boy 2 python firebase google-cloud-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()
Run Code Online (Sandbox Code Playgroud)

我尝试了这个但最终出现错误

AttributeError: AttributeError: 'DocumentSnapshot' object has no attribute '_document_path'
Run Code Online (Sandbox Code Playgroud)

寻求帮助!

Dou*_*son 5

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

  batch.delete(doc.reference)
Run Code Online (Sandbox Code Playgroud)