Firebase Firestore and Realtime Database: setting up Firebase project and Firestore database; basic CRUD operations on Firestore; structuring collections and subcollections

Firestore Firebase का NoSQL cloud database है: यह data को collections में grouped documents की तरह store करता है, जो subcollections में nest हो सकते हैं, और आपको add, read, update, और delete करने के लिए simple methods देता है, अक्सर आपके app को real time में update करते हुए।

11 min read · 7 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Firebase से एक Cloud Database

आप authentication और hosting के लिए पहले से Firebase इस्तेमाल कर चुके हैं। Firebase एक database भी offer करता है: Firestore, एक NoSQL, cloud-hosted store जो FestConnect directly इस्तेमाल कर सकता है, कभी-कभी एक separate MongoDB backend की बजाय, और जो data change होने के साथ app को real time में update कर सकता है।

Firestore का data model MongoDB से familiar लगेगा: collections में grouped documents। यह lesson इसे set up करना, इसका collections-and-documents structure (subcollections सहित), और basic CRUD cover करता है। यह FestConnect के data options को round out करता है।

Theory

Collections, Documents, Subcollections

Firestore data को एक clear hierarchy में organise करता है।

एक collection documents का एक group है ('events' सोचिए)। एक document fields (एक event का name, date, seats) और एक unique id वाला एक JSON-like record है। Crucially, एक document खुद subcollections contain कर सकता है, nested collections, तो एक event document कौन signed up हुआ इसका एक 'registrations' subcollection hold कर सकता है।

यह nesting आपको related data naturally model करने देता है: main entities के लिए top-level collections, एक specific document से belong करने वाली चीज़ों के लिए subcollections। यह एक NoSQL model है, flexible और JSON-shaped, MongoDB जैसा पर पूरी तरह Firebase से hosted और managed।

Practical

Firestore CRUD (Modular Web SDK)

import { getFirestore, collection, addDoc,
         getDocs, updateDoc, deleteDoc, doc } from 'firebase/firestore';

const db = getFirestore();

// create: add a document to the 'events' collection
await addDoc(collection(db, 'events'), { name: 'Robotics', seats: 50 });

// read: get all documents in the collection
const snap = await getDocs(collection(db, 'events'));
snap.forEach(d => console.log(d.id, d.data()));

// update / delete a specific document by id
await updateDoc(doc(db, 'events', id), { seats: 40 });
await deleteDoc(doc(db, 'events', id));

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Formula

Firestore vs Realtime Database

Firebase actually दो databases offer करता है। Firestore newer, document-and-collection store है ज़्यादा richer querying और structure के साथ, ज़्यादातर apps के लिए recommended default। Realtime Database older वाला है, जो सब कुछ एक बड़े JSON tree की तरह store करता है; यह basic real-time syncing के लिए simple और बहुत fast है पर data grow होने के साथ structure और query करना harder है।

दोनों live updates push कर सकते हैं, पर Firestore का organised model (collections, documents, subcollections) better scale करता है। FestConnect के लिए, Firestore sensible choice है। जानिए दोनों exist करते हैं, और Firestore modern, structured option है।

Quiz

Firestore में, data कैसे organised है?

  1. SQL जैसी fixed rows और columns वाली tables की तरह
  2. Documents (JSON-like records) के collections की तरह, जहाँ एक document nested subcollections contain कर सकता है
  3. एक single flat text file की तरह
  4. सिर्फ़ images की तरह
Show the answer

Documents (JSON-like records) के collections की तरह, जहाँ एक document nested subcollections contain कर सकता है

Firestore एक document NoSQL database है: data documents (fields और एक id वाले JSON-like records) के collections में रहता है, और एक document subcollections contain कर सकता है, natural nesting allow करते हुए (जैसे एक registrations subcollection वाला एक event document)। Option A rigid tables, rows, और columns वाला एक relational/SQL model describe करता है, जो यह नहीं है Firestore (एक NoSQL store) कैसे काम करता है। Option C wrong है: यह एक structured, queryable database है, एक single flat file नहीं। Option D nonsense है; Firestore किसी भी kind का structured data store करता है, सिर्फ़ images नहीं। Hierarchy याद रखिए: collections documents contain करते हैं, और documents subcollections contain कर सकते हैं।

Think first

'Real-Time' एक Normal Request के Comparison में Actually क्या देता है?

Firestore आपके app को real time में update कर सकता है। यह एक normal request से data fetch करने से कैसे अलग है, और यह क्यों matter करता है? फिर tap कीजिए।

Show the answer

एक normal request एक ONE-TIME pull है: आप data मांगते हैं, आपको इसकी current state मिलती है, और आप done हैं, अगर data बाद में change होता है, आपके app को तब तक पता नहीं चलता जब तक यह फिर से न पूछे। Real-time listening (Firestore का onSnapshot) एक standing SUBSCRIPTION है: आप Firestore को बताते हैं 'जब भी यह data change हो मुझे notify कीजिए', और यह हर update automatically आपके app को PUSH करता है, तो आपका UI database के साथ live sync में रहता है, बिना आपके poll किए। यह क्यों matter करता है: collaborative या live features के लिए, यह transformative है। Imagine कीजिए FestConnect दिखा रहा है एक event के लिए कितनी seats बची हैं। One-time fetches के साथ, एक user की screen कह सकती है '5 seats left' बहुत देर बाद जब वे already सब दूसरों ने ले ली हों, stale और misleading, जब तक user manually refresh न करे। एक real-time listener के साथ, instant कोई register करता है और count गिरती है, हर viewer की screen खुद-ब-खुद update होती है '4 left' तक, फिर '3', एक always-current view देते हुए। यही same power live chats, collaborative documents, dashboards, और notifications drive करती है, कहीं भी जहाँ कई users या devices को promptly latest state देखनी पड़े। Trade-off यह है कि live listeners एक connection consume करते हैं और manage होने चाहिए (जब ज़रूरत न हो unsubscribed), और हर screen को real-time नहीं चाहिए। पर जब freshness matter करती है, real-time updates एक static snapshot को एक living view में बदल देते हैं, जो Firebase की standout strengths में से एक है। एक बार subscribe कीजिए, और data खुद को current रखता है।

Summary

Key takeaways

  • Firestore Firebase का document-based NoSQL cloud database है।
  • Data documents (JSON-like records) के collections की तरह organised है; एक document nested subcollections contain कर सकता है।
  • एक Firebase project set up कीजिए, Firestore enable कीजिए, फिर CRUD के लिए SDK इस्तेमाल कीजिए।
  • CRUD: addDoc/setDoc (create), getDocs/getDoc (read), updateDoc (update), deleteDoc (delete)।
  • Firestore real-time updates push कर सकता है (onSnapshot) तो UI database के साथ live sync में रहता है।
  • Firebase का older Realtime Database एक JSON tree store करता है; Firestore newer, ज़्यादा structured, recommended option है।
  • Memory hook: collections documents hold करते हैं, documents subcollections hold कर सकते हैं; Firestore app को real time में update कर सकता है।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Firebase and React Integration

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati