How to Generate Auto ID in Firestore: Simple Guide
In Firestore, you can generate an automatic unique ID by using the
add() method on a collection, which creates a new document with a generated ID. Alternatively, use doc() without arguments to get a reference with an auto-generated ID before setting data.Syntax
Firestore provides two main ways to generate an automatic ID for a new document:
collectionRef.add(data): Adds a new document with an auto-generated ID and sets the data.collectionRef.doc(): Creates a document reference with a new auto-generated ID without data. You can then useset()to add data.
Both methods ensure unique IDs without manual input.
javascript
const docRef = collectionRef.doc(); const autoId = docRef.id; await docRef.set(data); // Or simpler: await collectionRef.add(data);
Example
This example shows how to add a new user document with an auto-generated ID using add(). It also shows how to get the ID when using doc() and set().
javascript
import { getFirestore, collection, addDoc, doc, setDoc } from 'firebase/firestore'; const db = getFirestore(); const usersRef = collection(db, 'users'); // Using add() to generate ID and add data async function addUser() { const userData = { name: 'Alice', age: 30 }; const docRef = await addDoc(usersRef, userData); console.log('User added with ID:', docRef.id); } // Using doc() to get auto ID, then set data async function addUserWithDoc() { const newDocRef = doc(usersRef); // auto ID generated const userData = { name: 'Bob', age: 25 }; await setDoc(newDocRef, userData); console.log('User added with ID:', newDocRef.id); } addUser(); addUserWithDoc();
Output
User added with ID: XyZ123abc
User added with ID: AbC456def
Common Pitfalls
Common mistakes when generating auto IDs in Firestore include:
- Trying to set a document with an empty string or manual ID instead of letting Firestore generate it.
- Using
doc(id)with a fixed string which can cause ID collisions. - Not awaiting asynchronous calls like
addDoc()orsetDoc(), leading to unexpected behavior.
Always use add() or doc() without parameters for unique IDs and await the promises.
javascript
/* Wrong way: manually setting empty string ID */ const wrongDocRef = collectionRef.doc(''); // Not recommended await wrongDocRef.set(data); /* Right way: auto ID generation */ const rightDocRef = collectionRef.doc(); await rightDocRef.set(data);
Quick Reference
Summary tips for generating auto IDs in Firestore:
- Use
collectionRef.add(data)for quick add with auto ID. - Use
collectionRef.doc()to get a new ID reference before setting data. - Always await asynchronous Firestore calls.
- Avoid manual or empty string IDs to prevent collisions.
Key Takeaways
Use collectionRef.add(data) to create a document with an auto-generated ID easily.
Use collectionRef.doc() without arguments to get a new document reference with an auto ID before setting data.
Always await Firestore async calls like addDoc() and setDoc() to ensure proper execution.
Avoid manually setting document IDs as empty strings or fixed values to prevent conflicts.
Auto-generated IDs are unique and safe for concurrent writes.