firestore update()函数将更新的数据投射到地图 [英] firestore update() function cast updated data to map

查看:53
本文介绍了firestore update()函数将更新的数据投射到地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新我的Firestore数据库中的某些数据,并且其中的数据是 Map< String,List< String>> ,换句话说,是列表的映射.因此,当我尝试更新地图内的列表时,会将列表投射到地图上.

I'm trying to update some data in my firestore database and the data inside is Map<String,List<String>> in other words, a map of lists. so when I'm trying to update a list inside the map, it casting the list to a map.

我的代码:

  Calendar calendar = Calendar.getInstance();
  String currentDate =  DateFormat.getDateInstance().format(calendar.getTime());
  DocumentReference docRef = db.collection("incomes").
     document(firebaseAuth.getCurrentUser().getUid());
 docRef.update(
   index + ".0", incomeTitleEt.getText().toString(),
   index + ".1", incomeAmountEt.getText().toString(),
   Integer.toString(index)+".2",currentDate
 ).addOnCompleteListener(new OnCompleteListener<Void>() {
   @Override
   public void onComplete(@NonNull Task<Void> task) {
   }
});

推荐答案

无法通过其索引更新数组字段中的项目.相反,您将必须:

There is no way to update an item in an array field by its index. Instead you'll have to:

  1. 将整个数组读入您的应用程序代码
  2. 更新内存中的数组
  3. 将整个更新后的阵列写回到数据库中

例外情况是当您的数组包含唯一的无序值时,例如当您存储帖子的标签数组时.在这种情况下,您可以使用 arrayUnion 运算符来操纵数组.但是,您的情况看起来有所不同,因此您必须使用上述步骤.

The exception to this is when your array contains unique, unordered values, such as when you for example store an array of the tags for a post. In that case you can use the arrayUnion operator to manipulate the array. However your case looks different, so you'll have to use the above steps.

这个问题经常出现,所以我建议阅读其中一些:

This questions comes up quite regularly, so I recommend reading some of these:

这篇关于firestore update()函数将更新的数据投射到地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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