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:
- Read the entire document into your application.
- Modify the item in the array in your application code.
- 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:
- Read the document.
- Get the entire array from that document.
- Modify the array in your application code.
- 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
Make React Useeffect Hook Not Run on Initial Render
How to Split the Ng-Repeat Data with Three Columns Using Bootstrap
How to Make Exe Files from a Node.Js App
How to Change Href of <A> Tag on Button Click Through JavaScript
Is There Ever a Good Reason to Pass a String to Settimeout
How to Determine Which Submit Button Was Pressed, Form Onsubmit Event, Without Jquery
Check If File Has Changed Using HTML5 File API
Vue.Js Get Selected Option on @Change
How to Determine If a Checkbox Is Checked
Why How to Use a Function Before It's Defined in JavaScript
JavaScript Function Declaration and Evaluation Order
How to Call Reduce on an Array of Objects to Sum Their Properties
JavaScript Require() Function Giving Referenceerror: Require Is Not Defined
JavaScript "Addeventlistener" Event Fires on Page Load
How to Get Image from Canvas Element and Use It in Img Src Tag
How to Pass Variable Value Between Different HTML Pages in JavaScript