firestore update()函数将更新的数据投射到地图 [英] firestore update() function cast updated data to map
问题描述
我正在尝试更新我的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:
- 将整个数组读入您的应用程序代码
- 更新内存中的数组
- 将整个更新后的阵列写回到数据库中
例外情况是当您的数组包含唯一的无序值时,例如当您存储帖子的标签数组时.在这种情况下,您可以使用 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中更新我的地图的某个数组条目
- 如何在消防站?
- 如何在Firestore中更新数组元素使用Android?
- 如何在android的Cloud Firestore中更新arrayList
这篇关于firestore update()函数将更新的数据投射到地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!