Firestore, a NoSQL document store

3 min read 4 hours ago
Published on Oct 16, 2025 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial provides a comprehensive guide to using Firestore, a NoSQL document store offered by Google. Firestore allows developers to store, sync, and query data for mobile and web applications. This guide will walk you through the key features of Firestore, how to set it up, and practical advice for using it effectively.

Step 1: Set Up Your Firestore Database

To start using Firestore, follow these steps:

  1. Create a Firebase Project

    • Go to the Firebase Console.
    • Click on "Add project" and fill in the necessary details.
    • Enable Google Analytics if needed, then click "Create project."
  2. Access Firestore

    • In your Firebase project dashboard, navigate to the "Firestore Database" section.
    • Click on "Create database."
    • Choose between "Start in test mode" or "Start in production mode." Test mode allows for easier initial development.
  3. Choose a Location

    • Select a location for your Firestore database to optimize latency and performance.

Step 2: Understand Firestore Structure

Firestore organizes data differently than traditional SQL databases. Familiarize yourself with these key concepts:

  • Collections

    • Groups of documents. Each collection can contain multiple documents.
  • Documents

    • Individual records within a collection. Documents can store various data types such as strings, numbers, booleans, and arrays.
  • Fields

    • Key-value pairs within a document. Each field can hold different data types.

Step 3: Add Data to Firestore

To add data to your Firestore database, use these methods:

  1. Using the Firebase Console

    • In the Firestore Database tab, click on "Start collection."
    • Enter a collection name and click "Next."
    • Add document fields and values, then click "Save."
  2. Using Code

    • Integrate Firebase SDK into your application. For example, using JavaScript:
    import { getFirestore, collection, addDoc } from "firebase/firestore"; 
    const db = getFirestore();
    
    async function addData() {
        try {
            const docRef = await addDoc(collection(db, "users"), {
                name: "John Doe",
                age: 30,
                email: "john.doe@example.com"
            });
            console.log("Document written with ID: ", docRef.id);
        } catch (e) {
            console.error("Error adding document: ", e);
        }
    }
    

Step 4: Querying Data

To retrieve data from Firestore, you can perform queries. Here’s how:

  1. Basic Query

    • Use the following code snippet to fetch documents:
    import { getFirestore, collection, getDocs } from "firebase/firestore"; 
    const db = getFirestore();
    
    async function fetchData() {
        const querySnapshot = await getDocs(collection(db, "users"));
        querySnapshot.forEach((doc) => {
            console.log(`${doc.id} => ${JSON.stringify(doc.data())}`);
        });
    }
    
  2. Filtering Data

    • To filter documents based on certain criteria, use query constraints.
    import { query, where } from "firebase/firestore";
    
    const q = query(collection(db, "users"), where("age", ">", 20));
    

Step 5: Security Rules

Setting up security rules is crucial to protect your data. Consider the following steps:

  1. Access Control

    • Navigate to the "Rules" tab in Firestore.
    • Define rules that specify who can read and write to your database.
  2. Example Rule

    • Allow read access only to authenticated users:
    service cloud.firestore {
        match /databases/{database}/documents {
            match /{document=**} {
                allow read, write: if request.auth != null;
            }
        }
    }
    

Conclusion

In this tutorial, you learned how to set up Firestore, understand its structure, add and query data, and implement security rules. Firestore offers great flexibility and powerful features for application development. As next steps, explore advanced querying capabilities and integrate Firestore with your frontend frameworks for a seamless user experience.