1

I have a document with these param:

"rent": "text",
"leases": [
 {
  "id": 1,
  "title": "text",
 },
 {
  "id": 2,
  "title": "text",
 }
]

I add new elements to the array like this:

.update({'leases': FieldValue.arrayUnion([newLease])});

But how can I update that id 2 only?

Dani
  • 3,128
  • 2
  • 43
  • 91

1 Answers1

8

The array operations can either add an item that isn't in the array yet (FieldValue.arrayUnion) or remove an item that is in the array already (FieldValue.arrayRemove ). There is no way to atomically update an existing item, by its index or otherwise.

So that means the only option is to:

  1. Read the document into your application code.
  2. Get the array from it.
  3. Update the array in your application.
  4. Write back the entire array to the database.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807