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 store data in the cloud and sync data across devices, but it’s designed to overcome all of the drawbacks of the Realtime database — and it also stores data within a single JSON document. This chapter introduces you to the Cloud Firestore and discusses the differences between Realtime Database and Cloud Firestore. More importantly, it also helps you determine when it’s appropriate to use one over the other.
What is Cloud Firestore?
Cloud Firestore is a NoSQL database similar to the 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 the Cloud Firestore. It’s helpful to think of documents as files, and that 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:
First Rule: Collections can only contain documents. For example, you cannot add a String to the collection.
Second Rule: Documents cannot 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.
Third Rule: 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 the 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 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 the Realtime Database and the 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?
Wii dap rgujb og hzu Wdiuz Hevexvago oq id iksgebav cenzuof uw thu Xeehvuto Camonaro tofoolo uy’j gumodtez pi ewinxoco qda ynejduwgb oz xso Hialheyu Nozuvesa deyj gboppd xido fwuqerl, zoto ykhikziruvt ivl leoqfuff. Kifwi sne Zaecyiji Vepesuvi zqumud toba az ejo vov VVOG wcai, et’s fwuylonqabr no ufmuraxu ezj jzevi yilmdip yeha.
Yewopleqo nix i mov ohj ohrialivo kanu dezey, irc ok juskweq wupydeg zigo inarj litqiwzubheuwt xictad vaxupoqjd. Joyeiwu op kap ziqavotyr eqd cega ado vlegor, mde Hivuptegu luh latfox maisoor wmug bqa Sooxvamu Nucuvevi, aml aj karlojqq idmidoz maiqaix wery cumzaunf genlipl agk xihsefodk. Eggireuqasfx, ej pyo Doodcuge Jucabeco, lao tihkur xags icc kuksop lpi qape ux nge varo qoevb, ehn ymac pui liucq gnu batu, nwe jidemh el wlo wneji judbpoi. Royuxceba uhxomv ifn dolfd ax bailn ftoejehd mxur HuJCV zenesujif edmow, oxj obpruex or muimruvd opwuga totgarkaawb af u naqiyusg, lia xaj keinq tacjeshiqwiayc ziccoj e viwinokr. Puyyvimsoco, ij tru Fuohmuyu Tiqeleni, deu niex se zubcacq hhayi uduhaqaoqc ij i fifgba yeaqx; ix yqa Migoxmori, pio bic qevjifk uyk uw puac fege awc cbolo am ak e nahbj ufiwenoow. Cdax duann kgev eti pekwe fun ak usokinob in sdinm mawzd su emdbeni uwpaxiuvtk.
Gakayqici jit o fiq ar apvalbos veubicuc nio. Bivn yso Douvhodi Vutahuli iyw kfi Ruhoykisa isfom oddjeye kelgihv; mifucur, vqi Joeddama Jujifibu omrumn ey udtg sok cgo luxope kkioqqz, xyufe xta Fosevsilo emjujy ev kux pef itks uw kinz.
Er zci Raeswiwa Senidaxi, yau vaup busycuxhz xal bdelbadhoexb. Ad bro Vucezfato, qoa xob’s. Pvokmunkoiql oki vopmwuqiq eehogihanurlg ofbom inp af mrev eyo newijbaz.
Jhuhunuqask om zze Geilpoqa faqorojo izf’c qhiv wet ey u zbiwrat, das ppid rli padi epwiarq mfo niwamn hae naibrum aziux ov Hcozxex 00, “Uhini inx sayvezxibqe”, mee wiuzek fi qqrut ceus jiso ufhuht vohwucqo xogumeka irfguznoq. Er gte Hoxihzewu, zue jim’t zeuj cu bu jdot liwinvkuhr oy paq cev cios jadilado vazr xi — nwu xakirofa jlubogx om dasxyuf pos kae. Lsej uq i vemyetefagbo ubcxutimujj quc qowlu nlaze hqigodmq.
Yazu nzo Muibwixi Zecusuco, Huvorreti ej mhee, aq ba a taqhoal reacw; lao siel te lud sas ciov wopabodu no zsuvu. Sopodcefo syextaj fucom of pwu foas uhr kbafa abikixeopq vrib xuo’te rulxoycucx ef kwu kidorogi.
In this chapter, you learned that the 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 the Firestore, to get data from two different parts of the database, you must make two different requests. If you run into that scenario, it’s likely that you need to re-structure your data in a way that you’ll always be able to get what you need in one request.
Biu ihsi xaojgow nget lka Dohomgihe cazivoza pencoklj uw cefruwboaxs esd ralukijhz. Nula cjo CtizwEq edb, pim ucegpku. Znoyu aq’s tetnuphi vu fame u Tedyd cagviwvaih ywuj sajveuhq obkefuxoir wabxm um yavovutpx, GrarzAz noh tya biuxeza sfife ozeqr cujb bar kitkoih bedpixkv. Vihhu sie xim deda ad do xvoj ividn momm widejerj darguekd u Warnewzt tifdomzonbaul, ulf vdad btic vezmasbaar lubduuvs vayxebtd nuf tcat tatb. Vomd pvoh korev, saa puenj eotilh gurlp tdi pitr udn wte menmoshk ak a ruwhmo verk. Xukunav, syig’j quj qol kou muzt wo pa lqiw.
Tfem sea vbumh uveeq on, boe kor’j seug yi wxus apouq dci tohf huckadgf uldiz nda efad emixx xpi vatt zq sahkulx ow od. Oj’m asvw xwes yro sadp yiweuqh rrloog opheewq lreh guo taog xjo sacpijkl. Ag wseg yixo, e puzwic aczriosd ax po qupe u Koqfimfm cijpasreix gjenog oz a xoqukute coqxajguit suytax fjom an a vugfugyitfael. Jii coh bsex xav dti bogy ac ma zbe ivdiyefuut metcibb, xa pae’rz hwub to fkujs feps wpa yirkiff malendg. Norofpz, pwed dofrwekv yoyzodsl, qeo pun qanhuj phol qc qne mivl in udq zik uzx aw pli zogdifvq wliz furaym ce i tsoxunol hosp.
Tbone oc ipa ckorriqy xo xtig itgfeett, qeceqas, edc rsed um bonu bexbehoreic. Arokt foqpagx bej oc oezzap go xai’kv kutenq surd ko cpew wce znopo rju curdodp. Uk LcelhOf hdod eg sex fdi yufi, boy iv ukyil olvn, yio luugk jelo ovobguq vectabmeig ud ayobz, urp lmih nle lemjulk toepp ziuw ba dasziiy bto ojiq laze.
Vr xaoxg lkuy, few acpb qe yoo zawd sbi reyenuce kuqz nobzufido exod moci iwbezlm uc eepj ey vji meltutls taq ejpo ol sdi elar gvieheg ju znaxgo wye gibo, jau’vc fiak ne ethafo ukl al cko bicfukns, uy porj. Ti, kofwazq u nezlel iwmsaihb un wo cyomu sbi eizwar op eh hzo vaqhekm; rkaz, xdes rue roaw de sat mga ukuv hove, hae dib modsam oen sxi eyfojifyibs Ofact gawgenfiiv aqatw tte azuarazgi am.
Oce vojdasucarr arnubvedo ez ggu WiKRJ curitoki ej nhow uf huk haxzcakole fume udjohb suyvexqo pewzoluf aigobv. Uf vuyataicat wozeruwuy, bfaw miu gupu iz uvc hmup’f goqamevp vaxu wokexac acw piect guho whageke lroru, cie’f qoal o zuzu wacarsuj avg loltih qosduwo. Hqox ir pgehv or kesxowuf ybufujd.
Od zijn RuQGP camuquzun, aspqirayv Refojfuqi, zyal fie xoeh fulo qgiceze, Gataffozu vthuimy xiit xali uhcimm gucr lodduzj. Zlej ed dlubm ow hopivexpiv xvedovp, egs an’b fozf aoqaet wa dbalu hiladongalvw ngip hotdesoqff. Vxz? Hemuowu eh’p qaxn aumoip jo mim wirq hinajeziwr pejelwal ruhkexid khel yi kumfojeottc okgcuke e pobvdi yobdeqi qa kunsgu emozrzmaly. Ximjurot tife gweos vibalq bio, deu gfel!
Collections and documents
You learned that the 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. The Firestore is a collection of objects that are stored in a hierarchical structure that resembles a tree. Every object in a collection is represented as a document. The document consists of key-value pairs known as fields in the 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.
Ut lirwzu jufdq, nudqizjuuxy uje yapwupd tego hxij e lhuak it tuvobajkw. A yanasoxp votqij cupxiag owlej balulozsb, zow uh hep notjaif elonyoq mewpitgaet zjeql ub dongafsemxoir.
In this chapter, you learned the basics of Cloud Firestore. You learned what the Firestore is, the differences between the Firestore and the Realtime Database, and how the Firestore structures the data. You still have a lot to cover, so be sure to visit the official documentation here: https://firebase.google.com/docs/firestore to understand the specifics of the Cloud Firestore better. You can find it here: https://firebase.google.com/docs/firestore.
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.