Categories
JavaScript Answers

How to update an array of objects with JavaScript Firestore?

Spread the love

To update an array of objects with JavaScript Firestore, we call the set method.

For instance, we write

firebase
  .firestore()
  .collection("proprietary")
  .doc(docID)
  .set(
    { sharedWith: [{ who: "abc@test.com", when: new Date() }] },
    { merge: true }
  );

to get the proprietary collection with collection.

We get the document with docID with doc.

And we call set to set the sharedWith property to an array with the object we want to add.

We set merge to true to append the item to the existing sharedWith property array in the document.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *