Firestore数据库:更新丢失的文档

问题描述:

我已经在测试新的Firestore API,并且在处理文档和调用update时遇到了一些问题。Firestore数据库:更新丢失的文档

我试图更新一个没有现有文件,我收到一个错误。显然这没问题,因为文档说如果DocumentReference不存在,update调用将会失败。然而,读official documentation我看到的一个代码块:

// Update the population, creating the document if it 
// does not already exist. 
db.collection("cities").document("Beijing").update(
     new UpdateOptions().createIfMissing(), 
     "population", 
     21500000); 

我试图复制,但我没有找到UpdateOptions电话。此外,检查update的不同覆盖方法,这些调用不是构造函数。

我使用的是Firebase的11.4.2版本。有什么想法发生了什么?

非常感谢你提前。

Firestore API在测试版发布之前发生变化,UpdateOptions不再存在。如果您想合并域插入到文档中,可能会或可能不存在使用set,就像这样:

Map<String, Object> data = new HashMap<>(); 
data.put("population", 21500000); 

db.collection("cities").document("Beijing") 
    .set(data, SetOptions.merge()); 

不幸的是,我们的翻译文档目前已经过时,请参阅英文版本现在。