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

查看:117
本文介绍了如何使用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.我还将更新这些答案.

P.S. I will also update those answers.

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

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