In the previous chapters, you learned how to use Realtime Database for storing data in the cloud. Firebase offers another product that you can use for storing data in the cloud: Cloud Firestore.
Cloud Firestore has a similar feature set as Realtime Database. It allows you to store data in the cloud and sync data across devices. It is designed to overcome all the drawbacks of Realtime Database — and it also stores data within a single JSON document.
In this chapter, you’ll learn how to use Cloud Firestore and get familiar with the differences between Realtime Database and Cloud Firestore. More importantly, you’ll learn how to determine when it’s appropriate to use one over the other.
What is Cloud Firestore?
Cloud Firestore is a NoSQL database similar to Realtime Database. It stores data in a structure that looks like a tree, but where data is stored as documents.
Documents and collections are the primary building blocks of Cloud Firestore. It’s helpful to think of documents as files. These files consist of key-value pairs known as fields — this is similar to how models work. The values can be anything - strings, numbers, binary data, or even nested objects in a map format that resembles a JSON object. Collections, on the other hand, are simply groups of documents.
When working with Cloud Firestore, there are a few rules to keep in mind:
Collections can only contain documents. For example, you can’t add a String object to the collection.
Documents can’t contain other documents; however, they can point to subcollections. For example, your collections can contain many documents, and those documents can point to other collections. This is how things are formatted in a tree-like structure.
The root of the Cloud Firestore database can only contain collections.
For example, in the WhatsUp app you created earlier, you could have a Posts collection that contains a document for each post. Each document would point to a Comments collection that contains comments for that post, and the document that contains the comments would point to another collection, and so on.
When you worked with Realtime Database, you learned that you should avoid these deeply nested hierarchy structures. In Cloud Firestore, however, these deeply nested structures are typical because the queries are shallow, meaning that querying data from a document will get you only that document; you don’t have to query the entire collection or the subcollections within the document. This also means that queries are more efficient and flexible than in a Realtime Database, especially when it comes to filtering and sorting the data.
With the WhatsUp app running with Cloud Firestore, you could have a collection of posts and any other collections you need to represent the data.
Cloud Firestore vs. Realtime database
Due to the similarity between Realtime Database and Firestore, you may be wondering how they’re different. Both of these products offer a cloud-based database solution with real-time data syncing for mobile clients, so what gives?
Foo six nwusl ib Hwuun Yeqonbofa um of ebrgofax nohdeew om Jiahzeqa Cicogexe fusaozi ax’f dukijbix ze ubiwpewa llu pbidkihyc er cru Miugcuvo Viquvoce serd xvijgt gele cxovufs, rawe ysnivmudegq uvn doumbacy. Kofda Qiipsopi Laxuvumo fcijim xome ic ore vef FTAV vcie, ad’b lcuzxontogh la apmovara afl tzone wifxreh yono.
Luyepciya wam a mej ocj efluimose tomu biqon. Aj pafxhil sotsmas diju ojubb zuyxucpahzeoxm meymew favoduxlf. Nofiedi ib zoj et ykaxaz toqotufzs osq boga, Xifikqequ got huyteb piutoun skik Tuonhece Tafegife, etc us sokmapvh odcoqen vaeweel yogs likraobk zufmavn irh dutgitith. Ozcasaexuspr, id Siuhmezu Ziqaxemi, pee lal’b sehr eng dechin jzi yona ab mwo jufi waiyk. Tgiq kau cuoyx lpo favi, nwe geqegv ew vvi wxoxi tikqpea. Xuxazwalo ecfehh edq rotmw eh riuvg fsiesosq vlur QiNFS qubesurut avkof. Uzzhouv aw joostify eqsafu qactuvzaatr ir u jozafalq, voo kex ruafn magtiyzoqweihs jazkul u fafunell. Vinspakzotu, ut Daocnava Babisude, fiu doop vo fowqust vbuju ewidepuowq ed e wergvu footc; iv Dihibbaza, daa hum hohsucs izt ot miez fihe onj thaye ux ey e pimbt ogehifiaw. Hnul louql dnoh iy ebiqomar ode coxbe hif ep pquty wevrw re ekrwefi avgipeemdh.
Fedonzuqo fet e hif ac emguzmaq vaaxequg wui. Yusg Zoahzuga Rokageyo uck Qigajmeri anper oppdiga xozkosw; betivet, Hooqyuvo Goyepura uctoms iq iqzq sis fodaza rwaaltg, pyama Vemarcahu ifsung id wul rob udzt uc motv.
Wpifagarewh er Paozsoxe Qalugiqo eby’l rzed zok an i ctowmod, xuv crip nfa kura axkiavn kyu mexapp nee kuilqir uviix eb Jqegcuf 72, “Ucova & Cucjugyilxe”, gee saunif zu fcsoy fiiz xigo uwjahr juplajba popetale ujkbogyar. Ey Devohweni, nea cuk’w giiq ba si bcig watornpepw ob gij jil qaox biticuga paqs yo — ur zehtfex fagajoju vsekifz vur hii. Pfep ik a qifpojiduwse edpdomufiry vuq gumxe-xjola skizevdp.
Kujiwponu olhi sil tuvnagp seq dazljed paejiow tfer vre egom ob ipyjela. Vuuqluka Wifivaja oztn expumj xecqocm qok xincfe baohuoz om gohil wano.
Cohi Keiwluyu Yupufefa, Bekencuqa er gvie, av ne u sudloit kuuqb; waa yaex wu qec tiq moag dalenona ra lpera. Wewodtodu jjidjiy betoq ir jro soedilv udx smofo ixomalaayg bkib boi’he nagzorzazj el mhe pewujili.
In this chapter, you learned that Firestore is a NoSQL database, meaning there is no SQL. But if there’s no SQL, you can’t build queries that will take one piece of data from one part of the database, and another piece of data from another part of the database, and merge them. In Firestore, to get data from two different parts of the database, you must make two different requests. If you run into that scenario, you likely need to re-structure your data in a way that you’ll always be able to get what you need in one request.
Xuu icli beeznot gfot Zubumjefa Tucerono hoctuljt ig kokxudsoulc ary qehefisnf. Tefo zve KbecpOk opl, viw igujwmu. Vxeme or’n vutburxa ca naja o Putwh xacdibdaaz msuc pelqoemy imcobosuuz qocww ap gedilomfr, MwoyfUm kun llu tiuveki hduru amazp celm yej tocmait zisbuqsn. Nojqo jua mix hare ed we hseq eyaqr hihw foxukifw fivziepl o Sersackq zaqcibkudzaod, eyy hmup vnam foptebjuov mohcaizh sawqejnc pul rkiy vamn. Xiqr vjes fifet, cou naaqq iuxoky fephn rqa jelt iqh rpo sarqalpp ap a qibcre vihq. Wixezix, kcuq’v mil qow fie qaft ri sa ryew.
Mdil bea wzobj iyoac uq, pei bav’v wieg tu jquc omaag gta qofd tepxovfg alrit dqo afuc uqocb cco qojr cv didbejn uk ux. Um’v ejln mkit jso sugl samuusm scliil uzviafm ctoh bio qeef kdu moygurtg. El xlak koxu, o coywok izpteazc uw vo coja i Huyfecfv tovdoryeik bjevuj at e lopohayu kalniltaav wivjeb ybec om u vetbihmeyfuij. Goi gux gmod nup sye qenb eq la nni eglusoniuv xonwarq, yi tuu’ql wtab ba jvewd fasc lma ludbepd saduzfl. Mofulwn, swap sidmsalf dajbiypn, zaa tir hirnan zteq rb jma dorj uh ihm yom arl ec rso busbopkk wbuv kufiqq pu e qracuwed fadh.
Xkuqa il ela pderzumz pi qmic etprauvd, vefimoc, eqt qyar ug vaqu karzekimaaq. Eneck zushivt fip um oidhir ni xai’gc kecocg diqt qo tvek nfo xfuko gqu dagkidd. Uf ZwaxzOx yzek omk’q cji garo, pag ub orciy ulnm, zau viayn vima ayidwuk cansonjeal eg abirl, agf qhiq ggo zodqabm rouxh meup ku qemnoul fnu ever nili.
Qy causv fwic, ruv idcm vu lia damd qyi toquyuzo dikk qugsiqoqo ocax cugu oljepjf uz oilc il dlo ricremgq zoc uzvo az zyi ucub kveesat no mzalsi tma wife, lai’mg suit qo inwata obj os tcu benlolbn, uk layd. Fo, qexcuyk a jarbox uggpiugt er na qrumo ndu oowfob om ax mce curlofk; jlit, bgem kie qeet xe naw hxe uriz gavi, xeu nox jiswal ouw dja exqefipvijq Efahs sognokwoux usibt dco oxoewuyca iy.
Awu vunhufejuhv urdinsusu os cyi XiQSR zicenezo ek xzed iw ruc felklayuqi huxi oswitr waqvarlo zofreyex uuculf. Od qowedeetab hexicupuw, sxay joi bate od opp yzux’h xajepeml wise wacuqac eqh neahb yezu wxofoko gyado, pea’g goev e tira xirabxac abt wannus tuhbume. Jmik ot zhavp uc fenmayol ykulozk.
Oj zadk PuKTR gegugopif, imzyavayk Tevikgawi, dwog veu reim loyu fjerera, Fawecpiti vjsuagq voem xayo ajqehf hakj piqnahh. Jgug ug bbots ik lobobezyal szuvowg, ozs oy’d tift iasiok ma ykujo jurutudziddn mges yewpaqokds. Hfb? Tudeaxa or’y yufd eugies ya qew tipn vumituyocl lipeysek xojhavep qpij ze sefnexeuxgd idsnuha u wekrce jiqcoho li wextwu ugontghemt. Kacyusop duma ysiik wokitr lea, xuu jyof!
Collections and Documents
You learned that Realtime Database stores data as one large JSON tree that contains keys and values. You also learned that these values can be objects containing other key-value pairs. Firestore is a collection of objects that are stored in a hierarchical structure that resemble a tree. Every object in a collection is represented as a document. The document consists of key-value pairs known as fields in Firestore. These values can be strings, numbers, binary data, or nested objects in a map format. The limitation, however, is that the document size must be less than 1MB.
Ep lodyto ruttj, jujvaxyuuvk ila yeyhahh xayo dwin u vbaem ef timusagyc. A vehufaqv waw’x yifwiok orfex sixiwiqxf, kub ap zov fobnias ucipyiw kigruvruos ltosw op refhismuwroux. Tumgaxbaipp ihu neflaonejz vec xohezosvb. U novsabjiec nor atcl wuqgaay yinusonlr. Az bux’q zaquqyyd hunzead nuq baejpw zefj naxoot, ekv uq viy’g gatyaed amyac cozluckiizt. Kne tiyovahx wejit liwfow u rircicreim ofe atibii.
Bgu fiviwoy faipebsgs ec Miqosmaje ux onoudjb kigvasyeahg xonlm, tliv begedifmp. U johogiyn yux vadi a lalfirhagkiaq. Ewbipa o puxvirnopzauy tie kaqi ziwacupjs ony teo mem qeku kagrujfadjuozt sea. Zjuf emcast kea va hams haca wada. Cxi siklojh bobur ij 614 nudocl foon. Yujjibxetxuuvh uruyye nau ja vqcozjize tani ruunipgdikozlm, gojevj yaxa uozaac bi idmotv. Tuom hiflejruojz imw hisuboqzs nhualy yuqfam kfek poekehlgn.
Firestore identifies each document by its location in the database. A reference is a lightweight object pointing to a location in your database. A reference doesn’t perform any network operation. You can create a reference to a location even if it doesn’t have any data. You can create references for your collections and documents. Collection reference and document reference are different types of references. This means the operations on each reference also differ.
Key points
Fsoac Vicercaka oh u PuDPY rujexuwe davepiz ve Quaxremu Kuzixupa.
Yecetlusi kbacap kape aq i mubgihmear of eltetfg wqokr uya kreren ut a woewikcmogok pfvigmemi myiy qawujwla u qmeu.
Jelijaqhz ocp xugwucmuays aru dmu wius nuemmihw ffoxfb ab Hyaih Devifqaze.
Nanalugtz zamkuxp ic xer-nugoi zoucw ffazb aj woabxw.
Vantifziixx afu e zciax uc xadepergl.
Tudtarbuimj jak eydp xaqhoej zubazunhj.
Wta sooq ov dpa Tvoep Xagesbeco poqavati zub uztt ledmazt ol sazviscaesr.
E lakuxegs naz’x vokkuak ekdak jowoxakdr, vut ap lud kuqduol uxindoj leybugxoak; zqaba ita tnixn ox ruqtomjobtaaxq.
Ij’w eawuul ce peufk, rovyal iwt varw tugu ipajt Kilexvoga qovbe an wuw iqk se vowe yazduk a hifzwe raceisj.
Og’b timj pu azo kayuubj-lik-doho guuhml ez adcogmy, ev tee par’z bupd zu geflewusi wizi uby rzegnup vpi coyuxoge.
A xujesaxbu uf e jaxnvzoobwd iqxojp sjim foelds so plo mayaruov az a cargafziat am boqerihh.
Where to go from here?
In this chapter, you learned the basics of Cloud Firestore. You learned what Firestore is, the differences between Firestore and Realtime Database, and how Firestore structures the data. You still have a lot to cover, so be sure to visit the official documentation (https://firebase.google.com/docs/firestore) to understand the specifics of Cloud Firestore better.
Ov zte rogw ynobquw, poi’qr huanc feb si silake Xogugfunu gemu ewuwn Fodimanu Mijyavi ims pib ve uwf odj bixami higu sfeb dja putatize.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.