Skip to content
Advertisement

How to update array elements in Firestore with Android?

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

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!

Advertisement

Answer

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.

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.

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.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement