Is there any way to update a specific index from the array in Firebase/firestore?

 export const editComment = (comment) => {
 return (dispatch, getState, { getFirebase, getFirestore }) => {
    const firestore = getFirestore();


     comments:  <--- this is array of objects

    }).then(() => {
      dispatch({ type: 'EDIT_COMMENT' })
    }).catch((error) => {
      dispatch({ type: 'EDIT_COMMENT_ERROR', error})

Solution 1

Is there any way to update a specific index from the array in Firestore?

No, there is not! This is not possible because if you want to perform an update, you need to know the index of that particular element. When talking about Cloud Firestore arrays, the things are different that you might think.

Because we are creating apps that can be used in a multi user environment, try to think what might happen if a user wants to edit a value at index 0, some other user wants to delete the value at index 0 and in the same time some other user might want to add another item at index 0. For sure, you'll end up having very different results and why not, get even array out of bounds exception. So Firestore actions with arrays are a little bit different. So you cannot perform actions like, insert, update or delete at a specific index.

If those two methods do not help you enough, you should get the entire document, get the array, modify it and add it back to the database.

Solution 2

Turn the array into it's own separate document and update the field in there. That's what I did. I had to restructure my database a bit in order to do so.

Solution 3

for me, I'm using firebase SDK for flutter, and nodeJS env. for cloud functions,, and I discovered the below solution.

this is a little bit of a hacky workaround

maybe we can save the list in a form of map not an array,, by using the array indexes as key strings to the sub maps

so you have structured the data like this in cloud fire store

key1: "value1" --> string
key2:          --> map
 > "0": {subKey1: "subValue1", subKey2: "subValue2"}
 > "1": {subKey3: "subValue3", subKey4: "subValue4"}
 > "2": {subKey5: "subValue5", subKey6: "subValue6"}

for example : if you want to update the subValue4, you may do this

await docReference.update({key2.1.subKey4: "theNewValueHere"});

and this ends up updating that document in cloud firestore to a document that looks like this

key1: "value1" --> string
key2:          --> map
 > "0": {subKey1: "subValue1", subKey2: "subValue2"}
 > "1": {subKey3: "subValue3", subKey4: "theNewValueHere"}
 > "2": {subKey5: "subValue5", subKey6: "subValue6"}

if you notice in firebase,, it updates the entire field key2 in this operation,, but gave you an easier access to update that index..

My problem is that I have to restructure all fields having list of maps to become maps within a map to have that access

And I will not do that,, will not refactor my entire db to do that,, I retrieve the entire doc,, (costs 1 read) modify at client and update the entire doc again (1 write)

I don't like it, but maybe someday I would use it