首页 > 解决方案 > 如何在不覆盖信息的情况下将哈希图添加到已经存在的哈希图中?

问题描述

我想在第一存储区中列出用户名列表(hashmap 中的 hashmap)。
我不想每次运行操作时都遍历参与者中的数据。

HashMap innermap = new HashMap();
HashMap outmap= new HashMap();
DocumentReference documentReference =Signup2.Getdatabase(Context).Collection("teams").Document(userdata.GetString("teamcode", "0000"))
.Collection("workout").Document(position.GetString("workoutdateposition", ""));
innermap.Put("username", userdata.GetString("username", ""));
outmap.Put("participants", innermap);
documentReference.Set(outmap);

标签: c#firebasegoogle-cloud-firestorexamarin.android

解决方案


如果要将 API 调用中的数据与文档中的现有数据合并,有两种选择:

  1. 调用UpdateAsyncAPI

     documentReference.UpdateAsync(outmap);
    
  2. UpdateAsync只有当文档已经存在时才有效。如果您不知道文档是否已经存在,您可以调用Setor SetAsyncwithSetOptions.MergeAll

     documentReference. SetAsync(outmap, SetOptions.MergeAll);
    

推荐阅读