1

I am doing a side project where I am trying to learn some about firestore, so i am making a hybrid shopping list/work hour tracker web app, here I want to ask about storing work hours in firestore. Here i have the picture showing the structure i have come up with for my work hour tracker. Work hour structure

Above you can see a date that is an object, or map as firestore calls it, which is a day. This day contains all the start and end dates of the work day(the user can toggle when he/she is working or not).
This way i can display a overall time worked by calculating the difference between "date_start" of the first object and the "date_end" of the last object, in the work_hours array. Also allowing me to visualise a more detailed list with the work hours.
Furthermore if the last object in the array does not have an "date_end" i can know for sure that i have to show a timer that is counting from the last "date_start" to now.

I have tried in multiple ways to update this array, but none worked like i intended them to, the closest i got was with upload method and specifying the path to "work_hours" map. but still i have to re-upload the whole modified array, which does not seem optimal to me... Sure it is not a huge operation, but it is a waste of resources in my mind, which is not necessary.

Now my question is: How to update just the "work_hours" array in this document field without needing to upload it again?
Alternatively: Any suggestions that are able to achieve a similar result or even better result, without needing to re-upload the whole map to the document, which have a different structure from mine.

Komi
  • 450
  • 5
  • 14
  • 1
    There is no way to make this any easier than what you're doing now. Updating an array item by index requires reading the document, modifying the array in memory, then updating the entire array back to the document. – Doug Stevenson Dec 29 '20 at 16:15
  • Okay, then, what is your suggestion on this? Maybe a better design for the storing of this data? Should i make a new collection with every entry? Or what could be better in your opinion? – Komi Dec 29 '20 at 21:02
  • 1
    I don't have an opinion. You can either keep doing what you're doing now, or change your schema to better suit your expected use cases. – Doug Stevenson Dec 29 '20 at 21:14

0 Answers0