Firestore Update Single Item in an Array Field

How to update an array of objects with Firestore?

Edit 08/13/2018: There is now support for native array operations in Cloud Firestore. See Doug's answer below.


There is currently no way to update a single array element (or add/remove a single element) in Cloud Firestore.

This code here:

firebase.firestore()
.collection('proprietary')
.doc(docID)
.set(
{ sharedWith: [{ who: "third@test.com", when: new Date() }] },
{ merge: true }
)

This says to set the document at proprietary/docID such that sharedWith = [{ who: "third@test.com", when: new Date() } but to not affect any existing document properties. It's very similar to the update() call you provided however the set() call with create the document if it does not exist while the update() call will fail.

So you have two options to achieve what you want.

Option 1 - Set the whole array

Call set() with the entire contents of the array, which will require reading the current data from the DB first. If you're concerned about concurrent updates you can do all of this in a transaction.

Option 2 - Use a subcollection

You could make sharedWith a subcollection of the main document. Then
adding a single item would look like this:

firebase.firestore()
.collection('proprietary')
.doc(docID)
.collection('sharedWith')
.add({ who: "third@test.com", when: new Date() })

Of course this comes with new limitations. You would not be able to query
documents based on who they are shared with, nor would you be able to
get the doc and all of the sharedWith data in a single operation.

Firestore Update single item in an array field

Firestore doesn't have the ability to update an existing element in an indexed array. Your only array options for updates are described in the documentation - you can add a new element to the array ("arrayUnion") or remove an element ("arrayRemove").

As an alternative, you can read the entire array out of the document, make modifications to it in memory, then update the modified array field entirely.

Firestore Update single item in array of objects

Whenever you want to modify individual elements of an array type field, you have to read the document, modify the array in memory, then update the modified array back to the document.

You will not be able to do this without reading the document first. If you require an atomic update, you can perform this read-then-update procedure in a transaction.

Trying to only update the specific array in Firestore but what it does is add more data in the array

There is no way to update an item in an array field with either arrayUnion or otherwise. You'll have to:

  1. Read the document into your application
  2. Get the full array
  3. Update the one item in the array
  4. Write the full array back to the document

Also see:

  • Firestore Update single item in an array field
  • Cloud firestore: Update field values in nested array object with dot notation
  • How can I update an object inside of an array in firestore?

How can I update an object inside of an array in firestore?

Firestore does not have an operation that allows you to update an existing item in an array by its index.

To update an existing item in the array, you will need to:

  1. Read the entire document into your application.
  2. Modify the item in the array in your application code.
  3. Write back the entire array to the document.

I'm pretty sure this has been asked before, so let me see if there's an answer with an example.

Also see:

  • How to remove an array element according to an especific key number?
  • Simple task list ordering - how to save it to Firebase Firestore?
  • How to update only a single value in an array
  • How to update an "array of objects" with Firestore?


Related Topics



Leave a reply



Submit