Home > Blockchain >  How to set nested data values of non-objects in firebase?
How to set nested data values of non-objects in firebase?

Time:07-25

I am currently making an application which stores data for each day of the year. I was thinking that the format would be something similar to:

[
  2022: {
    january: {
      week1: [
        {
          tasksCompleted: 5,
        },
        ... other weeks
     ],
     ... other months
  },
 }, ... other years
]

I am not too sure how I can store data of this format into firebase. I am totally open to using any other structures that accomplish this goal. How could I update the tasksCompleted property of a single day directly into the dataRef.update() without having to perform an extra database read which gets the data, updates it locally, then sets it in firebase?

CodePudding user response:

I'm sure you're read that you can't directly update a value nested in an array without reading, updating in memory, then writing back out. There are no workarounds for this.

Instead, you can store the items of the array as separate documents (perhaps as a nested subcollection). You will need to know the ID of the document that contains the item to update if you want to avoid an initial read in order to find the value to update. There are really no alternatives to this - you simply cannot update something by its integer index in Firestore, as these operations do not scale massively and efficiently in the way that Firestore requires.

Reading before updating is really not that bad of an option when you consider all of the massive scaling that Firestore gives you with no effort on your part.

  • Related