将空文档添加到集合

时间:2019-05-30 23:14:10

标签: java android google-cloud-firestore

我正在尝试将新文档和集合添加到我的数据库中。我知道我可以添加一个文档,如果不存在,是否应该添加该文档的集合。 我的问题是:如何将空文档添加到数据库中的新集合中?

我已经在android开发人员网站上尝试过此代码:

ApiFuture<WriteResult> future = db.collection("cities").document("LA");
// ...
// future.get() blocks on response
System.out.println("Update time : " + future.get().getUpdateTime());

但是它给了我我无法解决的ApiFuture部分错误。 所以我尝试了这个:

db.collection(mail).document("tarefa");

(邮件是字符串) 但这行不通。 有谁知道我该怎么办?

2 个答案:

答案 0 :(得分:1)

db.collection("cities").document("LA")仅创建对文档的引用。它尚未写入任何数据。

这可能是写空文档的最短方法:

db.collection("cities").document("LA").set(new HashMap<String, Object>())

如果这不起作用,那么update肯定应该起作用:

db.collection("cities").document("BJ")
    .set(new HashMap<String, Object>(), SetOptions.merge());

答案 1 :(得分:0)

因此,我改编了Frank Van Puffelen的答案,这解决了我的问题。这是我的操作方式:

db.collection(mail).document("tarefa").set(new HashMap<String, Object>());
相关问题