Ad

Only Update One Property Of The Collection's Field

Current application logic requires to store user records by years and will look something like:


balanceRecords: {
  2017: {
  ...,
    12: { ... }
  },
  2018: {
  ...,
    12: { ... }
  },
  2019: {
    ...,
    8: { ... }
  }
}

enter image description here

I've tried to update the document by ID inside the users collections

// year --> returned from the for loop operation from [2017, 2018, 2019]
firebaseDb.collection(`users`).doc(fyuAqykYpMVnB0h9grdznZgEcaU2)
            .update( personalData: {
               [year]: {}
            })
            .then(...)
            .catch(...)

Doing this I came to conclusion that each time I just overwrite the balanceRecords fields inside the document. Is it in general possible to update just a property of deep-nested field or it's expected that sub-collections are used instead?

Ad

Answer

First of all, doc(``) doesn't help anything. Just remove that.

You can use dot notation to update a deep property. I changed some of your sample code because it had syntax errors and apparently was updating a different property than the one you highlighted.

firebaseDb.collection('users').doc('fyuAqykYpMVnB0h9grdznZgEcaU2')
    .update( `balanceRecords.${year}`: {
        // whatever properties you want
    })
Ad
source: stackoverflow.com
Ad