简体   繁体   中英

how to delete documents in batch in firestore using python

I've a collection named XYZ in my firestore. And there are 500 documents with different fields in it. I have to delete multiple documents using a where clause from the collection.

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()

I tried this but ending up with an error

AttributeError: AttributeError: 'DocumentSnapshot' object has no attribute '_document_path'

Looking for help!

You are passing a DocumentSnapshot object to batch.delete() , which is not allowed. You must pass a DocumentReference object instead, which can be found in a property of a DocumentSnapshot.

  batch.delete(doc.reference)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM