0

How do I check if a firestore document exists without retrieving it? Either retrieve the list of keys or go doc by doc, I know all the key names I’m checking for. But if I retrieve the entire collection it takes too long because each has a lot of data. Using python 3.9

user433342
  • 859
  • 1
  • 7
  • 26

1 Answers1

0

You need to retrieve the document to check if it exists even if you know the key and it will cost you a read.

Although if you are using Admin SDK then you can use projections so the data you fetch will be a single field or the document IDs only as mentioned in this answer which might help you save some bandwidth.

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • I'm not seeing how to get just the document ids using the admin sdk. When I go to the python docs it basically just redirects me to the vanilla firestore client. Should I be looking at the firebase_admin.db module? – user433342 Aug 02 '21 at 02:34
  • @user433342 you are looking for [firestore module](https://firebase.google.com/docs/reference/admin/python/firebase_admin.firestore) When you fetch documents each of them will contain a DocumentReference from where you can get the ID. https://googleapis.dev/python/firestore/latest/document.html#google.cloud.firestore_v1.base_document.BaseDocumentReference.id I'm not sure about python but in NodeJS it's like `doc.ref.id` – Dharmaraj Aug 02 '21 at 04:32