如何使用 Android 更新 Firestore 中的数组元素? [英] How to update array elements in Firestore with Android?

查看:24
本文介绍了如何使用 Android 更新 Firestore 中的数组元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了这个发布和这个post 同一个人说你不能更新数组元素.

I read this post and this post of the same guy saying that you cannot update array elements.

我还阅读了最近的文章, 哪里说这可以用 arrayUnion 或类似的东西来完成,但我无法在文档中获得有关 stackoverflow 的任何信息.

I also read a recent article, where is says that this can be done somehow with arrayUnion or something like that but I cannot get any informations on stackoverflow either in the docs.

有什么办法可以解决这个问题吗?谢谢!

Is there any way I can solve this? Thanks!

推荐答案

实际上,写这些答案的人"就是我 :) 那个时候,没有一种方法可以更新数组元素.只能存储数组但不能更新数组成员.

Actually, the "guy" who wrote those answers it's me :) At that time, there wasn't a way in which you could update array elements. It was possible only to store arrays but not to update array members.

现在,在更新文档时,您可以将第二个参数作为第二个参数传递给 update() 方法:

Now, when updating a document, you can pass as the second argument to the update() method:

FieldValue.arrayUnion("newArrayValue")

您可以在官方文档中找到有关更新元素的更多信息在一个数组中.

You can find more informations in the official documentation regarding update elements in an array.

如果您的文档包含数组字段,您可以使用 arrayUnion() 和 arrayRemove() 添加和删除元素.arrayUnion() 向数组添加元素,但只添加不存在的元素.arrayRemove() 删除每个给定元素的所有实例.

If your document contains an array field, you can use arrayUnion() and arrayRemove() to add and remove elements. arrayUnion() adds elements to an array but only elements not already present. arrayRemove() removes all instances of each given element.

附言我也会更新这些答案.

P.S. I will also update those answers.

这篇关于如何使用 Android 更新 Firestore 中的数组元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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