# FIREBASE FIRESTORE ## Get DB Object ```jsx // Initialize Cloud Firestore and get a reference to the service import { getFirestore } from "firebase/firestore"; const db = getFirestore(app); ``` ## Create **Collection** and add a **Document** into it **Collection create automatically when you add a document to it** ```jsx import { collection, addDoc } from "firebase/firestore"; try { await addDoc(collection("DB OBJECT", "COLLECTION NAME"), {DOCUMENT}); } catch (e) { console.error("Error adding document: ", e); } ``` **EXAMPLE** ```jsx= import { collection, addDoc } from "firebase/firestore"; try { const docRef = await addDoc(collection(db, "users"), { first: "Ada", last: "Lovelace", born: 1815 }); console.log("Document written with ID: ", docRef.id); } catch (e) { console.error("Error adding document: ", e); } ``` **NOTE : Documents in a collection can contain different sets of information.** ## Read data from Firestore Retrive all collection documents ```jsx= getDocs(collection(db , 'collection name')) ``` **Example** ```jsx import { collection, getDocs } from "firebase/firestore"; const res = await getDocs(collection(db, "users")); res.forEach((doc) => { console.log(`${doc.id} => ${doc.data()}`); }); ``` ### HOW TO MACK A QUERY IN FIRESTORE ```jsx= const q = query(collection(db, "cities"), where("capital", "==", true)); // where(field , condition , condition value) ``` # REACT-FIREBASE-HOOKS ## FIRESTORE HOOKS 1.useCollection(firestore.Query)