问题描述
我正在使用 google.cloud 的 firestore 库,有没有办法在不检索所有数据的情况下检查文档是否存在?我试过
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()
但它返回 404 错误.(google.api_core.exceptions.NotFound)
but it returns a 404 error. (google.api_core.exceptions.NotFound)
然后我尝试了
fs.document("path/to/document").exists()
但存在"不是来自 DocumentReference 的函数.
but "exists" isn't a function from DocumentReference.
从源代码可以看出,exists() 是 DocumentSnapshot 类中的一个函数,函数 get() 应该返回一个 documentSnapshot.我不太确定我还能如何获得 DocumentSnapshot
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
谢谢
推荐答案
一种更简单且节省内存的方法:
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")
这篇关于如何知道firestore python中是否存在文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!