We'regoingtotalkaboutmodeling, specificallycausesoneofthethingsthatisprettychallengingaboutdocumentdatabaseisifyoucomefrom a relationaldatabaseworld, youprobablyknowaboutthirdnormalform.
Thisis a waytocarefullyandstructuredwaytoplanouthowyoumodelyourdataindocumentdatabaseisyoudon't reallyhavethat.
There's a lotMaursortofflexibilityinhowyoudesignthings.
Butit's challengingtoknowhowtomodelthingswhenyougetstarted, becausethere's somuchflexibilityandit's kindofopenendedfeels a littlemorelikeart.
It's inscienceandinsomesense, sowe'regonnafocusspecificallyontechniquesandtradeoffsandsomeguidelines I'vecomeupwithtohelpyoubesuccessfulmodelingyourdatawithdocumentdatabasessuchasMama D.
B.
Andthenwe'regonnastartwritingcodeforreallywe'regoingtouse a mongoodiumobjectdocumentmapperThinkoflikesequelalchemy.
ButfordocumentdatabaseiscalledMongoengine, andwe'regonnausethattocreatesomeclassestomodelordataandmapthoseclassestoandfromMongo, D B.
Well, firstofall, I hostthemostpopularpythonpodcastcalledTalkPythontome, and I'veinterviewedmany, manypeople, includingsomeofthefolksfromMongotbethecompanyaswellasauthorswho'vewrittenaboutmangaTVdesignpatternsandthingslikethat.
So I'vehad a lotofexperienceworkingwithpeopleinPythonAhmadiBe I'vecreatedtheTalkPythontrainingcompanyandwrittenmanypythoncoursesandmangaTVcoursesthereaswell.
Andalso, I'm partoftheMahdiBeMastersprogram.
Sothisis a groupofabout 35 externalcommunitymemberswhogiveadvicebacktoMongo.
TBand I'vebeenpartofthisformanyyears.
Workcloselywiththefolksinsidemonger D B overtheyears.
Sothisiswhatyouhaveinstoreforyou.
LotsofawesomemommytobeinPython.
I hopeyou'reexcited.
Let's getrighttoit.
Let's beginbylookingatwhyyoumightchoosemonger D B overothernosequeldatabases, otherdocumentdatabasesorevenotherrelationaldatabases.
SomangaTVisreally, reallypopularintermsof a databasethatpeopleareusing, andthat's greatbecausethatmeansit's welltestedwhenwegettosectionalbitlater, we'lllookatsomeoftheusersofmanga D B.
Sowehavetheseregularcolumntypepiecesofinformation I d entitleandsoon.
Wealsohavethisotherthing, theselectures.
NowtheselecturesairinthischapterrepresentedbythisrecordfromthiscourseandnoticethebracketandJavaScript, whichbasicallythisismeansthatis a array.
Andthearraycontains a bunchofsubobjects.
Sothisisreallyinteresting.
Insteadofjusthaving a chaptertableand a lecturetableondoing a jointorsomethinglikethat, a foreignkeyrelationship, we'reactuallyputtingthelecturesinsidethesamerecord.
Sothisisprettyinteresting.
Whenyoulookatitlikethis, youcanimaginethatthisislike a precomputerjoin.
If I do a queryforthelecture, say, given I d 1000 andoneon, I getthisrecordback, italreadyhasthelectures.
I don't havetomakeanotherroundtriptothedatabasetogetthem.
I don't havetodo a joinagainstseveraltablesinthatoriginalquery.
It's literally a primarykeyqueryagainstanindexprimarykeyandisinsanelyfast.
Thisistheworld's bestsellingsportsgamefranchise, andtheyrelyonMongo D B toscalethemillionsofplayersat.
It's reallyawesomeonemorebeforewemoveon.
Sotheseguysdownhere a littlefartheratsailthroughStillthroughis a companythatdoeslikeoutboundmaleandanalyticsandthingslikethat.
Sotheyare a marketingcompanyverymuchdoingtonsofemailtypeofstuff, like I said, andtheyhaveover 40 terabytesofdatain 100 and 20 physical, mostlyphysicalnudes, right?
So 120 servers, allworking, runningMargo D.
B insomegiantcluster, whichis a prettyawesomeusecase.
Sointhischapterwe'regonnagothroughoursnakebeingbeapplication, comeupwiththeentities, theclassesandtablesorcollectionsasthey'recalled a mangaTBandmodelthemout.
Sowe'regonnafirstlookathowthismodelinganddocumentdatabasescomparestotraditionalthirdnormalformmodelinginrelationaldatabaseswhenhe's a prettysimpleexample.
But I thinkyou'llgettheideaandyou'llseeitmuchmoreinactionwhenwebuildtherealapp.
Solet's takethissimplebookstoremodel.
Here.
Wehavebooks.
Theyhave a bunchofproperties.
Wehavepeoplepublishthosebooksnamedpublishers.
Theyhave a namewhentheywerefounded, andyoucannavigatethisforeignkeyrelationshipfrompublisherovertothepublisher I D onthebooknow.
Wealsohave a user, and a usermightrate a book.
Sowehaveusersandwehaveradiance.
Andwehave a foreignkeyrelationshipbetweenthemandtheyknowfromratingovertobook.
Wehave a onetoomanyrelationshipthere.
Write a book, canhavemanyratings.
Wehave a coupleofforeignkeyrelationshipsgoingonthisplace.
Now, letmetellyou, in a reallyapplication, there'llbemanymorelittletableswithextrainformationlikethis, likeratingsaboutbooksandsoonandsayreviews, forexample, thingslikethat.
ThisiswhatinMargaret's B I refertois a softforeignkeyconstraint.
There's a relationshipbetweenbooksandpublisher.
It's thepublisher I d settotheideaofthepublisher, butthedatabaseitselfdoesn't enforcethisright, Soyouneedtobe a littlemorecarefulinyourappandwe'llseehowwedothatwhenwegettothecoat.
Butas I wassayingaboutratings, thesewerenotgonnaputin a separatecollection.
Infact, we'regonnastorethoseinsideofourbookssowecanembedtheseobjectsand a raiseofeitherstraightvalueslikenumbersorstringsoractualsubdocuments.
Soagain, wecanthinkofthisradianceBeingembeddedwithinbooksas a precomputerjointis a slightbitofoverheadifyouactuallyoneofthebookwithouttheratingsandyou'rejustgonnagethimbackandignorethemmostofthetimeanyway, ifthatwasthecase, Butifmostofthetimeyoudowanttheradiance, thisis a hugespeedup.
Andlike I said, imaginetherewere 15 tablesbeforeandfiveofthemcouldbecollapsedintothebooks.
Theyhave a fivewayjoingoingdownto a singleprimarykeyquerywouldbeamazingaswediscussed, modelingwithdocumentsanddocumentdatabaseisis a littlebitmoreart.
There's a littlebitmoreflexibilityandkindofjustgutfeelofhowyoushoulddoit.
Butletmegiveyousomeguidelinesthatwillgiveyou a clearsetofconsiderations.
Soifourwebsitelikelistbooksandthatthatlistinghasthenumberofratingsandtheaverageratingandthingslikethatlistedaspartofthelisting, youpullup a book, maybetheratingsandthereviewsairshownrightthere.
There's waystoworkwithit, butyou'llseethatyouhavetodo a queeragainstthedatabaseandthen a littlebitoffilteringontheapplicationside, soit's notprohibitive.
Howmanyratingsmight a bookhave 10 ratingsah, 100 ratings.
1000 ratings.
Isthatnumbergonnajustgrow?
Growthenumberofratingsthatwehave?
Ifthiswaslikepageviewsanddetailsaboutthebrowseror I p addressanddateandtimeof a pagereviewed, I wouldnotmake a a goodcandidateforembeddingthat, likesaferviewsof a book, becausethatcouldjustgrowandgrowandgrow.
Isthepopularinyoursightgrows?
Anditcouldmakethedocumentssolargethatwhenyouretrieveitfromthedatabase, actually, thenetworktrafficandthedistractIQwouldbe a problem.
I don't reallyseethathappeningwithratings.
I mean, evenonAmazon, likesuperpopularbookshavehundreds, notmillionsofratings.
Soisprobablyokay.
Butifit's unfoundedset, youdonotwanttoembedit.
Andisthatboundsmallright?
Likethenmaybemillionsofviewsstillofbeingrecordedwithinsignof a bookwouldbe a reallybadidea, Andthereasonisthesedocumentsarelimitedto 16 megabytes, sonosinglerecord a mongrelyoubecanbelargerthan 16 megabytes, andthisisnot a limitationofmangaTV.
Thisisthemtryingtoprotectyoufromyourself.
Youdonotwanttogoon.
Justsayqueryby, say I ESPNandtrytopullback a bookandactuallyreadah, 100 megabytesoff a deskandoverthenetworkthatwoulddestroytheperformanceyourdatabase.
Allright, soifyou'realwaysgoingtosay I wouldliketoonmypagesshow a bookandit's relatedratings, youwouldabsolutelyputtheratingsinsidethebookbecausethatmeansyoujustdo a queryagainst a bookandyoualreadyhavethat, like, kindofprejoineddata.
Butifit's sortof a datawarehouseandyou'reaskingallkindsofquestionsfromalldifferentsortsofapplications, thentryandunderstandwhatwhatistherightwaytobuildmydocumentssoitmatches.
Thequeries I typicallydoaretheonesthat I needtobereallyquickandfast.
Onewaythatwehavebuiltapplicationstosharedataistojustsharethesamedatabase, sowemighthave a bunchofdifferentapplications, andthey'reallgoingtotalktothesamedatabase.
Andhereinthesecases, MargaretEveydocumentdatabasesmake a lotmoresense, andit's easiertodesignthedocumentsbecausetherangeofqueriesit's extremelyfocusedsoyoucantargetthoseparticularquestionsagainst a smallsetofqueriesguidelineswejusttalkedabout.
Sothisisthekindofmodelyouwannahave.
Ifyou'redoingdataexchangewithinyourorganizationandyou'reworkingwith a documentdatabase, I don't knowaboutyou, but I feellikewe'vetalkedaboutcodingandtalkedaboutMamaTBintheoryenough.
SohereweareagainandthesourcefolderSowe'llsayPythonthreedash M V Envy.
SorunthevirtualenvironmentmoduleintodotEnvythisnamingconventiondotenvysomethingthatPintArmunderstandswillautomaticallydetectandstartusingin a paststashedcopiesflagthat's onlyrequiredonMacOS, I believe.
Butanyway, we'llgowiththat.
Andifwedoin L S H, youcanseethishiddendotgov, butwedon't needtodoanythingelsewithit.
Andthisistypicallyprovidedbymonger d B thecompanyitself.
Sopiemongoisnodifferent.
It's thislowlevel, foundationalwaytotalktomongo D.
B.
Andyoudothisintheneedofquerysyntaxoffmonger D B thisJavaScriptJasonstyleofinteractingwiththedatabase.
Now, it's importanttoknowthatifyou'reworkingwithmongo d.
B intermsofrunningandmanaging, butfromwritingcode, we'regonnafocusedonsomethinghigherlevelandodiumsowecantakestructuredclassesandmappedthosetoandfromthedatabase.
I thinkthepodiummodelis a muchbetterwaytowriteyourapplication.
You'llseethere's notmuchstructurein a skiMelisdatabase, sohaving a littlebitofextrastructuredefinedbytheseclassesthatarepartofthissodiummodelreallyadds a lotofsafetytoyourapplication.
Inmyopinion, thepodiumwe'regonnauseforthiscourseismongoengine, andyoucanfinditshomepageanddetailsabout a documentation, andsoon.
Andthefirstthingthatwepassisanalias, so I'llberealexplicit.
Aliasequalscore.
Nowwhatisthisaliasthingabout?
Wecanhavemultipleconnectionstoevenmultipledatabasesorevenmultipledatabaseserversregisteredheresowecouldhave, like, a coredataandanalyticsforjustanalytics.
Thatgoesto a separatedatabasethatmaybehastonsmoredatabecauseit's likepageviewsandactionsandsoon.
Butwemightkeepthatseparatesowecanbackitupon a separateschedule, somethinglikethat.
Andthenweneedtosetthenameofthedatabase, andwe'llsetthattoSnake B and B do a quickformathere, andit's allgoodtogo.
Like I said, thisgetsmoreinterestinginrealconnections, butwe'regoingneedtocallthistotalktoMom.
It'd becourse, I actuallygointospendanhouragoinCreteOlynykserverandsetitupin a clusterofthedatabaseandthewebserversandthosekindsofthingsandreallymakethisworkperfectlyinsafely.
Butinthiscourse, we'renotgonnagointoit.
I justwanttoleaveyouwithyouneedtosetusup.
Youcanlookatthemonitor v dotorg's siteaswellamongyoutobedotcomsiteandgothroughthedocumentationonsomeofthestepsorjusttakemyothercourseifyou'rereallygonnagoandusethisinproduction.