Issue
im using the firestore library from google.cloud, is there any way to check if a document exists without retrieven all the data? i tried
fs.document("path/to/document").get().exists()
but it returns a 404 error. (google.api_core.exceptions.NotFound)
then i tried
fs.document("path/to/document").exists()
but "exists" isn't a function from DocumentReference.
I can see from the source that exists() is a function from the DocumentSnapshot Class, and the function get() should return a documentSnapshot. i'm not very sure how else i can get a DocumentSnapshot
Thank you
Solution
A much simpler and memory efficient approach:
doc_ref = db.collection('my_collection').document('my_document')
doc = doc_ref.get()
if doc.exists:
logging.info("Found")
else:
logging.info("Not found")
Note: this lazy loads the document by not fetching the content. So this is faster then getting the whole document. This does count as a single read.
Answered By - Cloudkollektiv
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.