Firestore仅更新一个字段 [英] Firestore update only one field

查看:48
本文介绍了Firestore仅更新一个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库.顺序为:集合-文档-哈希图.例如:

I have a database. The sequence is: collections - document - hashmaps. For example:

用户-集合的名称

users.uid-文档名称

users.uid - the name of the document

哈希图文档由很多哈希图用户数据组成 哈希图 哈希图 等等

Hashmap the document consists of a lot of hashmaps user data Hashmap Hashmap etc

哈希图:用户名是键,电话,位置等是值.我只需要为一个用户名更新一个字段(位置),但是不知道该怎么做?

Hashmap: the name of user is the key and telephone, location etc are the values. I need to update only one field(location) for one username, but can't understand how do this?

我尝试了另一种方式(更新alex的电话号码):

I tried the next way (update phone number for alex):

User user = new User();
user.setPhone(131902331);

Map<String,RealmObject> userMap = new HashMap<>();
userMap.put("alex",user);

          mFirebaseFirestore
                  .collection("users")
                  .document(mFirebaseAuth.getUid())
                  .set(userMap, SetOptions.merge())
                  .addOnSuccessListener(new OnSuccessListener<Void>() {
                      @Override
                      public void onSuccess(Void aVoid) {
                          LOG.info("Success");
                      }
                  })
                  .addOnFailureListener(new OnFailureListener() {
                      @Override
                      public void onFailure(@NonNull Exception e) {
                          LOG.error("Failure "+e.toString());
                      }
                  });

我做错了什么?

推荐答案

我知道这已经有将近一年的历史了,但这可能会对某人有所帮助.使用点符号

I know it's almost a year old question, but this might help someone. Use dot notation

db.collection("users")
  .document("frank")
  .update({
    "age": 13,
    "favorites.color": "Red"
  });

这篇关于Firestore仅更新一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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