1

I have some fields that needs to be collected only once when user sign up. After that, every week user have to submit a survey having 5 questions only. I am using firestore database to store my react native app data. I am storing data as follows

(Users)-->Email--> All other fields collected during sign up is stored as documents. Along with these documents (key:value) pairs I want to store the survey data as well which users will be submitting once a week.

Problem Below in figure is how I want to store my data. I have created a nested object WeeklySurvey and storing year, month, day as nested objects. I want to append new data here. For instance, the next data will be coming on 21. So, I want to append it under the month(11) as a separate object same as I am storing other day(15).

Whenever new data comes in it overwrites the whole object (WeeklySurvey) instead of appending. I don't want to create nested collections etc. I am not sure if creating nested fields is the only way forward.

enter image description here

For firestore I don't see anything related to appending data. we only have two options here SET, Update.

Any suggestions or solutions will be highly appreciated.

Below is my code to append/update code.

const addWeeklySurveyDataOnFirestore = (email) => {
        let dateObj = new Date();
        let year=dateObj.getFullYear();
        let day=dateObj.getDate();
        let month=dateObj.getMonth()+1;
        const surveyData={
             weeklySurvey:{
                [year]:{
                    [month]:{
                        [day]:{
                        "Exhause Rate":exhaustLevel,
                        "Pain Rate": painRate, 
                        'Practice Sessions':practiceSessions,
                        "Sleep Rate": sleepRate,
                        }
                    }
                }
            }  
            }
        firestore()
        .collection('users')
        .doc(email)
        .update(surveyData)
        .then(() => {
            console.log('User data added!');
        })
        .catch(() => {
            Alert.alert('Random Message')
        })
    }```
Muhammad Aqeel
  • 215
  • 2
  • 11
  • 1
    Seems the best way (from the duplicate) is to use dotted path notation with `update`, eg `update({ [\`weeklySurvey.${year}.${month}.${day}\`]: { "Exhaust Rate": ... }})` – Phil Nov 16 '22 at 06:17
  • Don't just thank me, add some upvotes on the answers in the other post – Phil Nov 16 '22 at 06:38

0 Answers0