0
0
FirebaseConceptBeginner · 3 min read

What is Cloud Firestore: Overview and Usage Guide

Cloud Firestore is a flexible, scalable database from Firebase that stores data in documents and collections in the cloud. It lets you sync and query data in real time across devices with simple APIs.
⚙️

How It Works

Imagine Cloud Firestore as a giant digital filing cabinet in the cloud. Instead of paper files, it stores data in documents, which are like individual folders holding information. These documents are grouped into collections, similar to drawers in the cabinet.

When your app needs data, it asks Firestore for specific documents or collections. Firestore quickly finds and sends the data back. It also keeps data updated in real time, so if one user changes something, others see the update instantly, like everyone sharing the same live document.

This system works over the internet, so your app can access data from anywhere, and Firestore handles storing, syncing, and securing the data automatically.

💻

Example

This example shows how to add and read data from Cloud Firestore using Firebase's JavaScript SDK.

javascript
import { initializeApp } from 'firebase/app';
import { getFirestore, collection, addDoc, getDocs } from 'firebase/firestore';

const firebaseConfig = {
  apiKey: 'YOUR_API_KEY',
  authDomain: 'YOUR_AUTH_DOMAIN',
  projectId: 'YOUR_PROJECT_ID'
};

const app = initializeApp(firebaseConfig);
const db = getFirestore(app);

async function addUser() {
  try {
    const docRef = await addDoc(collection(db, 'users'), {
      name: 'Alice',
      age: 30
    });
    console.log('Document written with ID:', docRef.id);
  } catch (e) {
    console.error('Error adding document:', e);
  }
}

async function readUsers() {
  const querySnapshot = await getDocs(collection(db, 'users'));
  querySnapshot.forEach((doc) => {
    console.log(`${doc.id} =>`, doc.data());
  });
}

addUser().then(() => readUsers());
Output
Document written with ID: <some_document_id> <some_document_id> => { name: 'Alice', age: 30 }
🎯

When to Use

Use Cloud Firestore when you want a cloud database that updates data instantly across users and devices. It is great for apps like chat apps, real-time collaboration tools, or any app needing live data syncing.

It works well when you want to avoid managing your own servers and want a database that scales automatically as your app grows.

Firestore also supports offline use, so your app can keep working even without internet, syncing changes when back online.

Key Points

  • Stores data in documents and collections, like folders and drawers.
  • Syncs data in real time across devices automatically.
  • Works offline and syncs when back online.
  • Scales easily without server management.
  • Uses simple APIs for easy integration.

Key Takeaways

Cloud Firestore is a cloud database that stores data in documents and collections.
It syncs data instantly across devices, enabling real-time updates.
Firestore works offline and syncs changes when the device reconnects.
It scales automatically, removing the need to manage servers.
Use Firestore for apps needing live data, like chat or collaboration tools.