How to update an "array of objects" with Firestore?
Asked 07 September, 2021
Viewed 1.7K times
  • 57
Votes

I'm currently trying Firestore, and I'm stuck at something very simple: "updating an array (aka a subdocument)".

My DB structure is super simple. For example:

proprietary: "John Doe",
sharedWith:
  [
    {who: "[email protected]", when:timestamp},
    {who: "[email protected]", when:timestamp},
  ],

I'm trying (without success) to push new records into shareWith array of objects.

I've tried:

// With SET
firebase.firestore()
.collection('proprietary')
.doc(docID)
.set(
  { sharedWith: [{ who: "[email protected]", when: new Date() }] },
  { merge: true }
)

// With UPDATE
firebase.firestore()
.collection('proprietary')
.doc(docID)
.update({ sharedWith: [{ who: "[email protected]", when: new Date() }] })

None works. These queries overwrite my array.

The answer might be simple, but I could'nt find it...

14 Answer