Learn how to retrieve Firestore document IDs with step-by-step instructions on initializing Firebase, fetching documents, and adding data with auto-generated or custom IDs.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1: Set up Firebase in your Project
Before you can access Firestore, you must set up Firebase in your project. First, ensure that you have a Firebase project created by going to the Firebase Console.
google-services.json file for Android or GoogleService-Info.plist for iOS and place it in your project according to the instructions.
Step 2: Initialize Firebase in Your Project
You need to initialize Firebase in your application to access its services like Firestore.
// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_AUTH_DOMAIN",
projectId: "YOUR_PROJECT_ID",
storageBucket: "YOUR_STORAGE_BUCKET",
messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
appId: "YOUR_APP_ID"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
// Initialize Firestore
const db = getFirestore(app);
Step 3: Get Document ID in Firestore
To retrieve document IDs in Firestore, you need to access documents within a collection. Assume a collection named users.
import { collection, getDocs } from "firebase/firestore";
const querySnapshot = await getDocs(collection(db, "users"));
querySnapshot.forEach((doc) => {
console.log(`${doc.id} => ${JSON.stringify(doc.data())}`);
});
In this snippet:
collection function is used to specify the Firestore collection.getDocs fetches all documents in the specified collection.forEach iterates through each document and retrieves the document ID using doc.id along with the document data using doc.data().
Step 4: Get Specific Document ID When Adding Data
When adding a new document to Firestore, the document ID can be automatically generated or specified by you.
To auto-generate a document ID:
import { collection, addDoc } from "firebase/firestore";
const docRef = await addDoc(collection(db, "users"), {
name: "John Doe",
email: "[email protected]"
});
console.log("Document ID: ", docRef.id);
To specify a document ID yourself:
import { doc, setDoc } from "firebase/firestore";
await setDoc(doc(db, "users", "customDocId"), {
name: "John Doe",
email: "[email protected]"
});
console.log("Document added with ID: customDocId");
Step 5: Conclusion
Understanding how to retrieve document IDs in Firestore is key to accessing and managing your data effectively. Whether you are iterating over collections or adding new entries, knowing how to handle document IDs allows for better data manipulation and querying. Make sure to handle asynchronous calls using async/await as demonstrated for smooth and efficient data operations.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.