如何知道firestore python中是否存在文档? [英] How to know if document exists in firestore python?

查看:23
本文介绍了如何知道firestore python中是否存在文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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中是否存在文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆