据我所知 - 为了检查 document
是否存在,我应该使用 get
函数并查看它是否存在。
我的问题是关于更新时的检查 - 有可能吗?(考虑到 document
可能会在更新发生时消失)。
我尝试更新一个空的 document
并收到一个错误,提示该文档不存在,但我认为这可能不是检查此问题的成功方法。
有几种方法可以将数据写入 Cloud Firestore:
SetOptions.merge()
。set()
但如果文档已存在则失败。因此,您只需要针对您的用例使用正确的操作即可。如果你想在不知道它是否存在的情况下更新一个文档,你想像这样使用带有 merge()
选项的 set()
(这个例子是 java ):
// Update one field, creating the document if it does not already exist.
Map<String, Object> data = new HashMap<>();
data.put("capital", true);
db.collection("cities").document("BJ")
.set(data, SetOptions.merge());
关于java - Firestore - 检查正在更新的文档是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46696463/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://www.ogeek.cn/) | Powered by Discuz! X3.4 |