2

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?

1 Answer 1

12

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:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.