Categories
JavaScript Answers

How to add Document with Custom ID to Firestore with JavaScript?

Spread the love

Sometimes, we want to add Document with Custom ID to Firestore with JavaScript

In this article, we’ll look at how to add Document with Custom ID to Firestore with JavaScript.

How to add Document with Custom ID to Firestore with JavaScript?

To add Document with Custom ID to Firestore with JavaScript, we call the set method.

For instance, we write

db.collection("cities").doc("LA").set({
  name: "Los Angeles",
  state: "CA",
  country: "USA",
});

to call set to add a document to the cities collection.

We call set with an object with the properties we want to add in a document.

We call doc with the ID of the document before we call set to populate its contents.

Conclusion

To add Document with Custom ID to Firestore with JavaScript, we call the set method.

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 *