How to Update an "Array of Objects" with Firestore

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.

How to update an array of objects with Firestore and Firebase functions?

You are converting it to a string by using stringify(). Try refactoring the code as shown below:

docRef.set({ testData : firestore.FieldValue.arrayUnion(...data) })
// use spread operator here ^^^

The documentation (NodeJS) has an example on adding multiple values using arrayUnion.

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?

How to update() an array of maps-objects on Firestore in Angular or Angularfirestore?

There is no way to change a value in an array. You'll either have to remove the old combination of values and add the new combination, or have to:

  1. Read the document.
  2. Get the entire array from that document.
  3. Modify the array in your application code.
  4. Write the modified array back to the database in its entirety.

Alternatively, you can store the lists in a map field rather than an array field, meaning that you give a name to each set of values - and you can the use dot notation to update the nested field.

How to update an array of Objects in Firebase Firestore?

The following should do the trick:

  db.collection('myCollection')
.doc('myID')
.update({
['details.descrip']: firebase.firestore.FieldValue.arrayUnion({
para: 'This will get added',
}),
});


Related Topics



Leave a reply



Submit