user3653771
user3653771

Reputation: 107

Can I update arrays in a batch?

Per the Firestore docs, I can add to an array:

regions: firebase.firestore.FieldValue.arrayUnion("greater_virginia")

And I can update a field through a batch:

batch.update(sfRef, {"population": 1000000});

But can I add to an array through a batch? Something like:

batch.update(userDoc, "arrayField": newElement)

e.g. I want to update 5 user documents. Each user document contains an array of tags and I want to push a new tag to each.

I cannot find any documentation on this - do I really need to create a transaction that reads the current value of the array and then writes the value back with the new tag?

Upvotes: 3

Views: 1165

Answers (2)

Dan Alboteanu
Dan Alboteanu

Reputation: 10232

for Android, here is how to update an array

  batch.update(ref, "myArrayFruits", FieldValue.arrayUnion("apple"))

Upvotes: 2

user3653771
user3653771

Reputation: 107

Thank you Doug for dignifying my question with a fair comment.

batch.update(sfRef, {regions:firebase.firestore.FieldValue.arrayUnion("greater_virginia")});

Upvotes: 0

Related Questions