Home > Back-end >  Add new document with random ID
Add new document with random ID

Time:10-02

How do I create a new document in a Firestore collection with a random ID? Is that even possible, or would it be better to generate an ID client side? There doesn't seem to be any docs for it and cannot find any answers online for v9.

CodePudding user response:

The documentation on adding documents has this pretty clear sample:

// Add a new document with a generated id.
const docRef = await addDoc(collection(db, "cities"), {
  name: "Tokyo",
  country: "Japan"
});

So the addDoc function adds a document with a newly generated ID.

  • Related