Categories
JavaScript Answers

How to add sub collection to a document in Firestore with JavaScript?

Spread the love

To add sub collection to a document in Firestore with JavaScript, we call add or set.

For instance, we write

db.collection("users").doc(username).collection("booksList").doc(myBookId).set({
  password,
  name,
  rollno,
});

to call set to update the entry in the 'booksList' collection with ID myBookId with the object we used as the argument for set.

And we write

db.collection("users").doc(username).collection("booksList").add({
  password,
  name,
  rollno,
});

to call add to add the entry we call add with to the 'booksList' collection.

By John Au-Yeung

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

Leave a Reply

Your email address will not be published.