Reading Data from Cloud Firestore
Reading data from Firestore involves querying collections and documents. You can fetch individual documents, entire collections, or perform queries with filters.
1. Basic Read Operations
Fetching a Single Document
To fetch a single document, use the getDoc method.
import { doc, getDoc } from "firebase/firestore";
// Create a reference to the user document
const userRef = doc(db, "users", "userId123");
// Fetch the document
const userSnap = await getDoc(userRef);
if (userSnap.exists()) {
console.log("User data:", userSnap.data());
} else {
console.log("No such document!");
}
Explanation:This code snippet shows how to fetch a specific document from Firestore. It creates a reference to a user document in the “users” collection and uses the `getDoc` method to retrieve it. If the document exists, it logs the user data; otherwise, it logs a message indicating that the document does not exist.
2. Fetching All Documents in a Collection
To fetch all documents in a collection, use the getDocs method.
import { collection, getDocs } from "firebase/firestore";
// Create a reference to the users collection
const usersRef = collection(db, "users");
// Fetch all documents
const querySnapshot = await getDocs(usersRef);
querySnapshot.forEach((doc) => {
console.log(`${doc.id} =>`, doc.data());
});
Explanation:This code snippet demonstrates how to fetch all documents from a Firestore collection. It creates a reference to the “users” collection and uses the `getDocs` method to retrieve all documents. It then iterates through the query snapshot, logging the ID and data of each document.
3. Querying Data
Firestore allows us to perform complex queries to fetch data based on specific criteria.
Example: Fetching Documents with a Condition
Suppose we want to fetch all users who signed up after a certain date.
import { collection, query, where, getDocs } from "firebase/firestore";
// Create a reference to the users collection
const usersRef = collection(db, "users");
// Create a query against the collection
const q = query(usersRef, where("createdAt", ">", "2023-01-01T00:00:00Z"));
// Fetch matching documents
const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
console.log(`${doc.id} =>`, doc.data());
});
Explanation:This code snippet demonstrates how to query a Firestore collection with a condition. It creates a reference to the “users” collection and constructs a query to fetch documents where the “createdAt” field is greater than January 1, 2023. It then retrieves and logs the matching documents.
4. Updating Data
To update an existing document, use the updateDoc method. This method only updates the specified fields, leaving other fields unchanged.
Example: Updating a User’s Email
import { doc, updateDoc } from "firebase/firestore";
// Create a reference to the user document
const userRef = doc(db, "users", "userId123");
// Update the user's email
await updateDoc(userRef, {
email: "new.email@example.com"
});
Explanation:This code snippet updates the email field of a user document with the ID “userId123” in the “users” collection. It uses the `updateDoc` method from Firestore to perform the update operation.
5. Deleting Data
To delete a document, use the deleteDoc method.
Example: Deleting a User
import { doc, deleteDoc } from "firebase/firestore";
// Create a reference to the user document
const userRef = doc(db, "users", "userId123");
// Delete the document
await deleteDoc(userRef);
Explanation:This code snippet deletes a user document with the ID “userId123” from the “users” collection. It uses the `deleteDoc` method from Firestore to perform the delete operation.
Writing and Reading Data in Cloud Firestore
Cloud Firestore is a NoSQL document database built for automatic scaling, high performance, and ease of application development. In this article, we will explore how to write and read data in Cloud Firestore along with complete with detailed examples and. Whether you are a beginner or looking to refine your understanding, this guide will walk you through the basics in a straightforward, easy-to-understand manner.