爱问 爱问共享资料 爱问分类
首页 > > MongoDB and Python.2011.pdf

MongoDB and Python.2011.pdf

MongoDB and Python.2011.pdf

上传者: tyhtzwl_fupgu
636次下载 0人收藏 暂无简介 简介 2012-04-03 举报

简介:no sql

MongoDBandPythonNiallO’HigginsBeijing•Cambridge•Farnham•Köln•Sebastopol•TokyoMongoDBandPythonbyNiallO’HigginsCopyright2011NiallO’Higgins.Allrightsreserved.PrintedintheUnitedStatesofAmerica.PublishedbyO’ReillyMedia,Inc.,1005GravensteinHighwayNorth,Sebastopol,CA95472.O’Reillybooksmaybepurchasedforeducational,business,orsalespromotionaluse.Onlineeditionsarealsoavailableformosttitles(http://my.safaribooksonline.com).Formoreinformation,contactourcorporate/institutionalsalesdepartment:(800)998-9938orcorporate@oreilly.com.Editors:MikeLoukidesandShawnWallaceProductionEditor:JasminePerezProofreader:O’ReillyProductionServicesCoverDesigner:KarenMontgomeryInteriorDesigner:DavidFutatoIllustrator:RobertRomanoNutshellHandbook,theNutshellHandbooklogo,andtheO’ReillylogoareregisteredtrademarksofO’ReillyMedia,Inc.MongoDBandPython,theimageofadwarfmongoose,andrelatedtradedressaretrademarksofO’ReillyMedia,Inc.Manyofthedesignationsusedbymanufacturersandsellerstodistinguishtheirproductsareclaimedastrademarks.Wherethosedesignationsappearinthisbook,andO’ReillyMedia,Inc.wasawareofatrademarkclaim,thedesignationshavebeenprintedincapsorinitialcaps.Whileeveryprecautionhasbeentakeninthepreparationofthisbook,thepublisherandauthorassumenoresponsibilityforerrorsoromissions,orfordamagesresultingfromtheuseoftheinformationcon-tainedherein.ISBN:978-1-449-31037-0[LSI]1315837615TableofContentsPreface......................................................................v1.GettingStarted.........................................................1Introduction1FindingReferenceDocumentation2InstallingMongoDB3RunningMongoDB5SettingupaPythonEnvironmentwithMongoDB62.ReadingandWritingtoMongoDBwithPython..............................9ConnectingtoMongoDBwithPython10GettingaDatabaseHandle11InsertingaDocumentintoaCollection12WritetoaCollectionSafelyandSynchronously13GuaranteeingWritestoMultipleDatabaseNodes14IntroductiontoMongoDBQueryLanguage15Reading,Counting,andSortingDocumentsinaCollection15UpdatingDocumentsinaCollection18DeletingDocumentsfromaCollection20MongoDBQueryOperators21MongoDBUpdateModifiers223.CommonMongoDBandPythonPatterns..................................23AUniquelyDocument-OrientedPattern:Embedding23FastLookups:UsingIndexeswithMongoDB29Location-basedAppswithMongoDB:GeoSpatialIndexing33CodeDefensivelytoAvoidKeyErrorsandOtherBugs37Update-or-Insert:UpsertsinMongoDB39AtomicRead-Write-Modify:MongoDB’sfindAndModify40FastAccountingPattern41iii4.MongoDBwithWebFrameworks.........................................45Pylons1.xandMongoDB45PyramidandMongoDB49DjangoandMongoDB51GoingFurther53iv|TableofContentsPrefaceI’vebeenbuildingproductiondatabase-drivenapplicationsforabout10years.I’veworkedwithmostoftheusualrelationaldatabases(MSSQLServer,MySQL,PostgreSQL)andwithsomeveryinterestingnonrelationaldatabases(Freebase.com’sGraphd/MQL,BerkeleyDB,MongoDB).MongoDBisatthispointthesystemIenjoyworkingwiththemost,andchooseformostprojects.Itsitssomewhereatacrossroadsbetweentheperformanceandpragmatismofarelationalsystemandtheflexibilityandexpressivenessofasemanticwebdatabase.Ithasbeencentraltomysuccessinbuildingsomequitecomplicatedsystemsinashortperiodoftime.IhopethatafterreadingthisbookyouwillfindMongoDBtobeapleasantdatabasetoworkwith,andonewhichdoesn’tgetinthewaybetweenyouandtheapplicationyouwishtobuild.ConventionsUsedinThisBookThefollowingtypographicalconventionsareusedinthisbook:ItalicIndicatesnewterms,URLs,emailaddresses,filenames,andfileextensions.ConstantwidthUsedforprogramlistings,aswellaswithinparagraphstorefertoprogramelementssuchasvariableorfunctionnames,databases,datatypes,environmentvariables,statements,andkeywords.ConstantwidthboldShowscommandsorothertextthatshouldbetypedliterallybytheuser.ConstantwidthitalicShowstextthatshouldbereplacedwithuser-suppliedvaluesorbyvaluesdeter-minedbycontext.vThisiconsignifiesatip,suggestion,orgeneralnote.Thisiconindicatesawarningorcaution.UsingCodeExamplesThisbookisheretohelpyougetyourjobdone.Ingeneral,youmayusethecodeinthisbookinyourprogramsanddocumentation.Youdonotneedtocontactusforpermissionunlessyou’rereproducingasignificantportionofthecode.Forexample,writingaprogramthatusesseveralchunksofcodefromthisbookdoesnotrequirepermission.SellingordistributingaCD-ROMofexamplesfromO’Reillybooksdoesrequirepermission.Answeringaquestionbycitingthisbookandquotingexamplecodedoesnotrequirepermission.Incorporatingasignificantamountofexamplecodefromthisbookintoyourproduct’sdocumentationdoesrequirepermission.Weappreciate,butdonotrequire,attribution.Anattributionusuallyincludesthetitle,author,publisher,andISBN.Forexample:“MongoDBandPythonbyNiallO’Higgins.Copyright2011O’ReillyMediaInc.,978-1-449-31037-0.”Ifyoufeelyouruseofcodeexamplesfallsoutsidefairuseorthepermissiongivenabove,feelfreetocontactusatpermissions@oreilly.com.SafariBooksOnlineSafariBooksOnlineisanon-demanddigitallibrarythatletsyoueasilysearchover7,500technologyandcreativereferencebooksandvideostofindtheanswersyouneedquickly.Withasubscription,youcanreadanypageandwatchanyvideofromourlibraryonline.Readbooksonyourcellphoneandmobiledevices.Accessnewtitlesbeforetheyareavailableforprint,andgetexclusiveaccesstomanuscriptsindevelopmentandpostfeedbackfortheauthors.Copyandpastecodesamples,organizeyourfavorites,down-loadchapters,bookmarkkeysections,createnotes,printoutpages,andbenefitfromtonsofothertime-savingfeatures.O’ReillyMediahasuploadedthisbooktotheSafariBooksOnlineservice.TohavefulldigitalaccesstothisbookandothersonsimilartopicsfromO’Reillyandotherpub-lishers,signupforfreeathttp://my.safaribooksonline.com.vi|PrefaceHowtoContactUsPleaseaddresscommentsandquestionsconcerningthisbooktothepublisher:O’ReillyMedia,Inc.1005GravensteinHighwayNorthSebastopol,CA95472800-998-9938(intheUnitedStatesorCanada)707-829-0515(internationalorlocal)707-829-0104(fax)Wehaveawebpageforthisbook,wherewelisterrata,examples,andanyadditionalinformation.Youcanaccessthispageat:http://www.oreilly.com/catalog/0636920021513Tocommentorasktechnicalquestionsaboutthisbook,sendemailto:bookquestions@oreilly.comFormoreinformationaboutourbooks,courses,conferences,andnews,seeourwebsiteathttp://www.oreilly.com.FindusonFacebook:http://facebook.com/oreillyFollowusonTwitter:http://twitter.com/oreillymediaWatchusonYouTube:http://www.youtube.com/oreillymediaAcknowledgmentsIwouldliketothankArielBackenroth,AseemMohantyandEugeneCiuranaforgivingdetailedfeedbackonthefirstdraftofthisbook.IwouldalsoliketothanktheO’Reillyteamformakingitagreatpleasuretowritethebook.Ofcourse,thankstoallthepeopleat10genwithoutwhomMongoDBwouldnotexistandthisbookwouldnothavebeenpossible.Preface|viiCHAPTER1GettingStartedIntroductionFirstreleasedin2009,MongoDBisrelativelynewonthedatabasescenecomparedtocontemporarygiantslikeOraclewhichtracetheirfirstreleasestothe1970’s.Asadocument-orienteddatabasegenerallygroupedintotheNoSQLcategory,itstandsoutamongdistributedkeyvaluestores,AmazonDynamoclonesandGoogleBigTablere-implementations.WithafocusonrichoperatorsupportandhighperformanceOnlineTransactionProcessing(OLTP),MongoDBisinmanywaysclosertoMySQLthantobatch-orienteddatabaseslikeHBase.ThekeydifferencesbetweenMongoDB’sdocument-orientedapproachandatradi-tionalrelationaldatabaseare:1.MongoDBdoesnotsupportjoins.2.MongoDBdoesnotsupporttransactions.Itdoeshavesomesupportforatomicoperations,however.3.MongoDBschemasareflexible.Notalldocumentsinacollectionmustadheretothesameschema.1and2areadirectresultofthehugedifficultiesinmakingthesefeaturesscaleacrossalargedistributedsystemwhilemaintainingacceptableperformance.Theyaretrade-offsmadeinordertoallowforhorizontalscalability.AlthoughMongoDBlacksjoins,itdoesintroducesomealternativecapabilites,e.g.embedding,whichcanbeusedtosolvemanyofthesamedatamodelingproblemsasjoins.Ofcourse,evenifembeddingdoesn’tquitework,youcanalwaysperformyourjoininapplicationcode,bymakingmultiplequeries.Thelackoftransactionscanbepainfulattimes,butfortunatelyMongoDBsupportsafairlydecentsetofatomicoperations.Fromthebasicatomicincrementanddecrementoperatorstothericher“findAndModify”,whichisessentiallyanatomicread-modify-writeoperator.1Itturnsoutthataflexibleschemacanbeverybeneficial,especiallywhenyouexpecttobeiteratingquickly.Whileupfrontschemadesign—asusedintherelationalmodel—hasitsplace,thereisoftenaheavycostintermsofmaintenance.Handlingschemaupdatesintherelationalworldisofcoursedoable,butcomeswithaprice.InMongoDB,youcanaddnewpropertiesatanytime,dynamically,withouthavingtoworryaboutALTERTABLEstatementsthatcantakehourstorunandcomplicateddatamigrationscripts.However,thisapproachdoescomewithitsowntradeoffs.Forexample,typeenforcementmustbecarefullyhandledbytheapplicationcode.Customdocumentversioningmightbedesirabletoavoidlargeconditionalblockstohandleheterogeneousdocumentsinthesamecollection.ThedynamicnatureofMongoDBlendsitselfquitenaturallytoworkingwithadynamiclanguagesuchasPython.ThetradeoffsbetweenadynamicallytypedlanguagesuchasPythonandastaticallytypedlanguagesuchasJavainmanyrespectsmirrorthetrade-offsbetweentheflexible,document-orientedmodelofMongoDBandtheup-frontandstaticallytypedschemadefinitionofSQLdatabases.PythonallowsyoutoexpressMongoDBdocumentsandqueriesnatively,throughtheuseofexistinglanguagefeatureslikenesteddictionariesandlists.IfyouhaveworkedwithJSONinPython,youwillimmediatelybecomfortablewithMongoDBdocumentsandqueries.Forthesereasons,MongoDBandPythonmakeapowerfulcombinationforrapid,iter-ativedevelopmentofhorizontallyscalablebackendapplications.ForthevastmajorityofmodernWebandmobileapplications,webelieveMongoDBislikelyabetterfitthanRDBMStechnology.FindingReferenceDocumentationMongoDB,Python,10gen’sPyMongodriverandeachoftheWebframeworksmen-tionedinthisbookallhavegoodreferencedocumentationonline.ForMongoDB,wewouldstronglysuggestbookmarkingandatleastskimmingovertheofficialMongoDBmanualwhichisavailableinafewdifferentformatsandcon-stantlyupdatedathttp://www.mongodb.org/display/DOCS/Manual.WhilethemanualdescribestheJavaScriptinterfaceviathemongoconsoleutilityasopposedtothePythoninterface,mostofthecodesnippetsshouldbeeasilyunderstoodbyaPythonprogram-merandmore-or-lessportabletoPyMongo,albeitsometimeswithalittlebitofwork.Furthermore,theMongoDBmanualgoesintogreaterdepthoncertainadvancedandtechnicalimplementationanddatabaseadministrationtopicsthanispossibleinthisbook.2|Chapter1: GettingStartedForthePythonlanguageandstandardlibrary,youcanusethehelp()functionintheinterpreterorthepydoctoolonthecommandlinetogetAPIdocumentationforanymethodsormodules.Forexample:pydocstringThelatestPythonlanguageandAPIdocumentationisalsoavailableforonlinebrowsingathttp://docs.python.org/.10gen’sPyMongodriverhasAPIdocumentationavailableonlinetogowitheachre-lease.Youcanfindthisathttp://api.mongodb.org/python/.Additionally,onceyouhavethePyMongodriverpackageinstalledonyoursystem,asummaryversionoftheAPIdocumentationshouldbeavailabletoyouinthePythoninterpreterviathehelp()function.Duetoanissuewiththevirtualenvtoolmentionedinthenextsection,“py-doc”doesnotworkinsideavirtualenvironment.Youmustinsteadrunpython-mpydocpymongo.InstallingMongoDBForthepurposesofdevelopment,itisrecommendedtorunaMongoDBserveronyourlocalmachine.Thiswillpermityoutoiteratequicklyandtrynewthingswithoutfearofdestroyingaproductiondatabase.Additionally,youwillbeabletodevelopwithMongoDBevenwithoutanInternetconnection.Dependingonyouroperatingsystem,youmayhavemultipleoptionsforhowtoinstallMongoDBlocally.MostmodernUNIX-likesystemswillhaveaversionofMongoDBavailableintheirpackagemanagementsystem.ThisincludesFreeBSD,Debian,Ubuntu,Fedora,Cen-tOSandArchLinux.Installingoneofthesepackagesislikelythemostconvenientap-proach,althoughtheversionofMongoDBprovidedbyyourpackagingvendormaylagbehindthelatestreleasefrom10gen.Forlocaldevelopment,aslongasyouhavethelatestmajorrelease,youareprobablyfine.10genalsoprovidestheirownMongoDBpackagesformanysystemswhichtheyupdateveryquicklyoneachrelease.Thesecanbealittlemoreworktogetinstalledbutensureyouarerunningthelatest-and-greatest.Aftertheinitialsetup,theyaretypicallytrivialtokeepup-to-date.Foraproductiondeployment,whereyoulikelywanttobeabletoupdatetothemostrecentstableMongoDBversionwithaminimumofhassle,thisoptionprobablymakesthemostsense.InadditiontothesystempackageversionsofMongoDB,10genprovidebinaryzipandtararchives.Theseareindependentofyoursystempackagemanagerandareprovidedinboth32-bitand64-bitflavoursforOSX,Windows,LinuxandSolaris.10genalsoprovidestatically-builtbinarydistributionsofthiskindforLinux,whichmaybeyourbestoptionifyouarestuckonanolder,legacyLinuxsystemlackingthemodernlibcInstallingMongoDB|3andotherlibraryversions.Also,ifyouareonOSX,WindowsorSolaris,theseareprobablyyourbestbet.Finally,youcanalwaysbuildyourownbinariesfromthesourcecode.UnlessyouneedtomakemodificationstoMongoDBinternalsyourself,thismethodisbestavoidedduetothetimeandcomplexityinvolved.Intheinterestsofsimplicity,wewillprovidethecommandsrequiredtoinstallastableversionofMongoDBusingthesystempackagemanagerofthemostcommonUNIX-likeoperatingsystems.Thisistheeasiestmethod,assumingyouareononeoftheseplatforms.ForMacOSXandWindows,weprovideinstructionstoinstallthebinarypackagesfrom10gen.Ubuntu/Debian:sudoapt-getupdate;sudoapt-getinstallmongodbFedora:sudoyuminstallmongo-stable-serverFreeBSD:sudopkg_add-rmongodbWindows:Gotohttp://www.mongodb.organddownloadthelatestproductionreleasezipfileforWindows—choosing32-bitor64-bitdependingonyoursystem.ExtractthecontentsofthezipfiletoalocationlikeC:\mongodbandaddthebindirectorytoyourPATH.MacOSX:Gotohttp://www.mongodb.organddownloadthelatestproductionreleasecompressedtarfileforOSX—choosing32-bitor64-bitdependingonyoursystem.Extractthecontentstoalocationlike/usr/local/or/optandaddthebindirectorytoyour$PATH.Forexmaple:cd/tmpwgethttp://fastdl.mongodb.org/osx/mongodb-osx-x86_64-1.8.3-rc1.tgztarxfzmongodb-osx-x86_64-1.8.3-rc1.tgzsudomkdir/usr/local/mongodbsudocp-rmongodb-osx-x86_64-1.8.3-rc1/bin/usr/local/mongodb/exportPATH=$PATH:/usr/local/mongodb/bin4|Chapter1: GettingStartedInstallMongoDBonOSXwithMacPortsIfyouwouldliketotryathird-partysystempackagemanagementsystemonMacOSX,youmayalsoinstallMongoDB(andPython,infact)throughMacPorts.MacPortsissimilartoFreeBSDports,butforOSX.Awordofwarningthough:MacPortscompilesfromsource,andsocantakeconsid-erablylongertoinstallsoftwarecomparedwithsimplygrabbingthebinaries.Futher-more,youwillneedtohaveApple’sXcodeDeveloperToolsinstalled,alongwiththeX11windowingenvironment.ThefirststepistoinstallMacPortsfromhttp://www.macports.org.WerecommenddownloadingandinstallingtheirDMGpackage.OnceyouhaveMacPortsinstalled,youcaninstallMongoDBwiththecommand:sudoportselfupdate;sudoportinstallmongodbToinstallPython2.7fromMacPortsusethecommand:sudoportselfupdate;sudoportinstallpython27RunningMongoDBOnsomeplatforms—suchasUbuntu—thepackagemanagerwillautomaticallystartthemongoddaemonforyou,andensureitstartsonbootalso.Onothers,suchasMacOSX,youmustwriteyourownscripttostartit,andmanuallyintegratewithlaunchdsothatitstartsonsystemboot.NotethatbeforeyoucanstartMongoDB,itsdataandlogdirectoriesmustexist.IfyouwishtohaveMongoDBstartautomaticallyonbootonWindows,10genhaveadocumentdescribinghowtosetthisupathttp://www.mongodb.org/display/DOCS/Windows+ServiceTohaveMongoDBstartautomaticallyonbootunderMacOSX,firstyouwillneedaplistfile.Savethefollowing(changingdbandlogpathsappropriately)to/Library/LaunchDaemons/org.mongodb.mongod.plist:<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEplistPUBLIC"-//Apple//DTDPLIST1.0//EN""http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plistversion="1.0"><dict><key>RunAtLoad</key><true/><key>Label</key><string>org.mongo.mongod</string><key>ProgramArguments</key><array><string>/usr/local/mongodb/bin/mongod</string><string>--dbpath</string>RunningMongoDB|5<string>/usr/local/mongodb/data/</string><string>--logpath</string><string>/usr/local/mongodb/log/mongodb.log</string></array></dict></plist>Nextrunthefollowingcommandstoactivatethestartupscriptwithlaunchd:sudolaunchctlload/Library/LaunchDaemons/org.mongodb.mongod.plistsudolaunchctlstartorg.mongodb.mongodAquickwaytotestwhetherthereisaMongoDBinstancealreadyrunningonyourlocalmachineistotypemongoatthecommand-line.ThiswillstarttheMongoDBadminconsole,whichattemptstoconnecttoadatabaseserverrunningonthedefaultport(27017).Inanycase,youcanalwaysstartMongoDBmanuallyfromthecommand-line.Thisisausefulthingtobefamiliarwithincaseyoueverwanttotestfeaturessuchasreplicasetsorshardingbyrunningmultiplemongodinstancesonyourlocalmachine.Assumingthemongodbinaryisinyour$PATH,run:mongod--logpath<path/to/mongo.logfile>--port<porttolistenon>--dbpath<path/to/datadirectory>SettingupaPythonEnvironmentwithMongoDBInordertobeabletoconnecttoMongoDBwithPython,youneedtoinstallthePy-Mongodriverpackage.InPython,thebestpracticeist

MongoDB and Python.2011.pdf

MongoDB and Python.2011.pdf

上传者: tyhtzwl_fupgu
636次下载 0人收藏 暂无简介 简介 2012-04-03 举报

简介:no sql

MongoDBandPythonNiallO’HigginsBeijing•Cambridge•Farnham•Köln•Sebastopol•TokyoMongoDBandPythonbyNiallO’HigginsCopyright2011NiallO’Higgins.Allrightsreserved.PrintedintheUnitedStatesofAmerica.PublishedbyO’ReillyMedia,Inc.,1005GravensteinHighwayNorth,Sebastopol,CA95472.O’Reillybooksmaybepurchasedforeducational,business,orsalespromotionaluse.Onlineeditionsarealsoavailableformosttitles(http://my.safaribooksonline.com).Formoreinformation,contactourcorporate/institutionalsalesdepartment:(800)998-9938orcorporate@oreilly.com.Editors:MikeLoukidesandShawnWallaceProductionEditor:JasminePerezProofreader:O’ReillyProductionServicesCoverDesigner:KarenMontgomeryInteriorDesigner:DavidFutatoIllustrator:RobertRomanoNutshellHandbook,theNutshellHandbooklogo,andtheO’ReillylogoareregisteredtrademarksofO’ReillyMedia,Inc.MongoDBandPython,theimageofadwarfmongoose,andrelatedtradedressaretrademarksofO’ReillyMedia,Inc.Manyofthedesignationsusedbymanufacturersandsellerstodistinguishtheirproductsareclaimedastrademarks.Wherethosedesignationsappearinthisbook,andO’ReillyMedia,Inc.wasawareofatrademarkclaim,thedesignationshavebeenprintedincapsorinitialcaps.Whileeveryprecautionhasbeentakeninthepreparationofthisbook,thepublisherandauthorassumenoresponsibilityforerrorsoromissions,orfordamagesresultingfromtheuseoftheinformationcon-tainedherein.ISBN:978-1-449-31037-0[LSI]1315837615TableofContentsPreface......................................................................v1.GettingStarted.........................................................1Introduction1FindingReferenceDocumentation2InstallingMongoDB3RunningMongoDB5SettingupaPythonEnvironmentwithMongoDB62.ReadingandWritingtoMongoDBwithPython..............................9ConnectingtoMongoDBwithPython10GettingaDatabaseHandle11InsertingaDocumentintoaCollection12WritetoaCollectionSafelyandSynchronously13GuaranteeingWritestoMultipleDatabaseNodes14IntroductiontoMongoDBQueryLanguage15Reading,Counting,andSortingDocumentsinaCollection15UpdatingDocumentsinaCollection18DeletingDocumentsfromaCollection20MongoDBQueryOperators21MongoDBUpdateModifiers223.CommonMongoDBandPythonPatterns..................................23AUniquelyDocument-OrientedPattern:Embedding23FastLookups:UsingIndexeswithMongoDB29Location-basedAppswithMongoDB:GeoSpatialIndexing33CodeDefensivelytoAvoidKeyErrorsandOtherBugs37Update-or-Insert:UpsertsinMongoDB39AtomicRead-Write-Modify:MongoDB’sfindAndModify40FastAccountingPattern41iii4.MongoDBwithWebFrameworks.........................................45Pylons1.xandMongoDB45PyramidandMongoDB49DjangoandMongoDB51GoingFurther53iv|TableofContentsPrefaceI’vebeenbuildingproductiondatabase-drivenapplicationsforabout10years.I’veworkedwithmostoftheusualrelationaldatabases(MSSQLServer,MySQL,PostgreSQL)andwithsomeveryinterestingnonrelationaldatabases(Freebase.com’sGraphd/MQL,BerkeleyDB,MongoDB).MongoDBisatthispointthesystemIenjoyworkingwiththemost,andchooseformostprojects.Itsitssomewhereatacrossroadsbetweentheperformanceandpragmatismofarelationalsystemandtheflexibilityandexpressivenessofasemanticwebdatabase.Ithasbeencentraltomysuccessinbuildingsomequitecomplicatedsystemsinashortperiodoftime.IhopethatafterreadingthisbookyouwillfindMongoDBtobeapleasantdatabasetoworkwith,andonewhichdoesn’tgetinthewaybetweenyouandtheapplicationyouwishtobuild.ConventionsUsedinThisBookThefollowingtypographicalconventionsareusedinthisbook:ItalicIndicatesnewterms,URLs,emailaddresses,filenames,andfileextensions.ConstantwidthUsedforprogramlistings,aswellaswithinparagraphstorefertoprogramelementssuchasvariableorfunctionnames,databases,datatypes,environmentvariables,statements,andkeywords.ConstantwidthboldShowscommandsorothertextthatshouldbetypedliterallybytheuser.ConstantwidthitalicShowstextthatshouldbereplacedwithuser-suppliedvaluesorbyvaluesdeter-minedbycontext.vThisiconsignifiesatip,suggestion,orgeneralnote.Thisiconindicatesawarningorcaution.UsingCodeExamplesThisbookisheretohelpyougetyourjobdone.Ingeneral,youmayusethecodeinthisbookinyourprogramsanddocumentation.Youdonotneedtocontactusforpermissionunlessyou’rereproducingasignificantportionofthecode.Forexample,writingaprogramthatusesseveralchunksofcodefromthisbookdoesnotrequirepermission.SellingordistributingaCD-ROMofexamplesfromO’Reillybooksdoesrequirepermission.Answeringaquestionbycitingthisbookandquotingexamplecodedoesnotrequirepermission.Incorporatingasignificantamountofexamplecodefromthisbookintoyourproduct’sdocumentationdoesrequirepermission.Weappreciate,butdonotrequire,attribution.Anattributionusuallyincludesthetitle,author,publisher,andISBN.Forexample:“MongoDBandPythonbyNiallO’Higgins.Copyright2011O’ReillyMediaInc.,978-1-449-31037-0.”Ifyoufeelyouruseofcodeexamplesfallsoutsidefairuseorthepermissiongivenabove,feelfreetocontactusatpermissions@oreilly.com.SafariBooksOnlineSafariBooksOnlineisanon-demanddigitallibrarythatletsyoueasilysearchover7,500technologyandcreativereferencebooksandvideostofindtheanswersyouneedquickly.Withasubscription,youcanreadanypageandwatchanyvideofromourlibraryonline.Readbooksonyourcellphoneandmobiledevices.Accessnewtitlesbeforetheyareavailableforprint,andgetexclusiveaccesstomanuscriptsindevelopmentandpostfeedbackfortheauthors.Copyandpastecodesamples,organizeyourfavorites,down-loadchapters,bookmarkkeysections,createnotes,printoutpages,andbenefitfromtonsofothertime-savingfeatures.O’ReillyMediahasuploadedthisbooktotheSafariBooksOnlineservice.TohavefulldigitalaccesstothisbookandothersonsimilartopicsfromO’Reillyandotherpub-lishers,signupforfreeathttp://my.safaribooksonline.com.vi|PrefaceHowtoContactUsPleaseaddresscommentsandquestionsconcerningthisbooktothepublisher:O’ReillyMedia,Inc.1005GravensteinHighwayNorthSebastopol,CA95472800-998-9938(intheUnitedStatesorCanada)707-829-0515(internationalorlocal)707-829-0104(fax)Wehaveawebpageforthisbook,wherewelisterrata,examples,andanyadditionalinformation.Youcanaccessthispageat:http://www.oreilly.com/catalog/0636920021513Tocommentorasktechnicalquestionsaboutthisbook,sendemailto:bookquestions@oreilly.comFormoreinformationaboutourbooks,courses,conferences,andnews,seeourwebsiteathttp://www.oreilly.com.FindusonFacebook:http://facebook.com/oreillyFollowusonTwitter:http://twitter.com/oreillymediaWatchusonYouTube:http://www.youtube.com/oreillymediaAcknowledgmentsIwouldliketothankArielBackenroth,AseemMohantyandEugeneCiuranaforgivingdetailedfeedbackonthefirstdraftofthisbook.IwouldalsoliketothanktheO’Reillyteamformakingitagreatpleasuretowritethebook.Ofcourse,thankstoallthepeopleat10genwithoutwhomMongoDBwouldnotexistandthisbookwouldnothavebeenpossible.Preface|viiCHAPTER1GettingStartedIntroductionFirstreleasedin2009,MongoDBisrelativelynewonthedatabasescenecomparedtocontemporarygiantslikeOraclewhichtracetheirfirstreleasestothe1970’s.Asadocument-orienteddatabasegenerallygroupedintotheNoSQLcategory,itstandsoutamongdistributedkeyvaluestores,AmazonDynamoclonesandGoogleBigTablere-implementations.WithafocusonrichoperatorsupportandhighperformanceOnlineTransactionProcessing(OLTP),MongoDBisinmanywaysclosertoMySQLthantobatch-orienteddatabaseslikeHBase.ThekeydifferencesbetweenMongoDB’sdocument-orientedapproachandatradi-tionalrelationaldatabaseare:1.MongoDBdoesnotsupportjoins.2.MongoDBdoesnotsupporttransactions.Itdoeshavesomesupportforatomicoperations,however.3.MongoDBschemasareflexible.Notalldocumentsinacollectionmustadheretothesameschema.1and2areadirectresultofthehugedifficultiesinmakingthesefeaturesscaleacrossalargedistributedsystemwhilemaintainingacceptableperformance.Theyaretrade-offsmadeinordertoallowforhorizontalscalability.AlthoughMongoDBlacksjoins,itdoesintroducesomealternativecapabilites,e.g.embedding,whichcanbeusedtosolvemanyofthesamedatamodelingproblemsasjoins.Ofcourse,evenifembeddingdoesn’tquitework,youcanalwaysperformyourjoininapplicationcode,bymakingmultiplequeries.Thelackoftransactionscanbepainfulattimes,butfortunatelyMongoDBsupportsafairlydecentsetofatomicoperations.Fromthebasicatomicincrementanddecrementoperatorstothericher“findAndModify”,whichisessentiallyanatomicread-modify-writeoperator.1Itturnsoutthataflexibleschemacanbeverybeneficial,especiallywhenyouexpecttobeiteratingquickly.Whileupfrontschemadesign—asusedintherelationalmodel—hasitsplace,thereisoftenaheavycostintermsofmaintenance.Handlingschemaupdatesintherelationalworldisofcoursedoable,butcomeswithaprice.InMongoDB,youcanaddnewpropertiesatanytime,dynamically,withouthavingtoworryaboutALTERTABLEstatementsthatcantakehourstorunandcomplicateddatamigrationscripts.However,thisapproachdoescomewithitsowntradeoffs.Forexample,typeenforcementmustbecarefullyhandledbytheapplicationcode.Customdocumentversioningmightbedesirabletoavoidlargeconditionalblockstohandleheterogeneousdocumentsinthesamecollection.ThedynamicnatureofMongoDBlendsitselfquitenaturallytoworkingwithadynamiclanguagesuchasPython.ThetradeoffsbetweenadynamicallytypedlanguagesuchasPythonandastaticallytypedlanguagesuchasJavainmanyrespectsmirrorthetrade-offsbetweentheflexible,document-orientedmodelofMongoDBandtheup-frontandstaticallytypedschemadefinitionofSQLdatabases.PythonallowsyoutoexpressMongoDBdocumentsandqueriesnatively,throughtheuseofexistinglanguagefeatureslikenesteddictionariesandlists.IfyouhaveworkedwithJSONinPython,youwillimmediatelybecomfortablewithMongoDBdocumentsandqueries.Forthesereasons,MongoDBandPythonmakeapowerfulcombinationforrapid,iter-ativedevelopmentofhorizontallyscalablebackendapplications.ForthevastmajorityofmodernWebandmobileapplications,webelieveMongoDBislikelyabetterfitthanRDBMStechnology.FindingReferenceDocumentationMongoDB,Python,10gen’sPyMongodriverandeachoftheWebframeworksmen-tionedinthisbookallhavegoodreferencedocumentationonline.ForMongoDB,wewouldstronglysuggestbookmarkingandatleastskimmingovertheofficialMongoDBmanualwhichisavailableinafewdifferentformatsandcon-stantlyupdatedathttp://www.mongodb.org/display/DOCS/Manual.WhilethemanualdescribestheJavaScriptinterfaceviathemongoconsoleutilityasopposedtothePythoninterface,mostofthecodesnippetsshouldbeeasilyunderstoodbyaPythonprogram-merandmore-or-lessportabletoPyMongo,albeitsometimeswithalittlebitofwork.Furthermore,theMongoDBmanualgoesintogreaterdepthoncertainadvancedandtechnicalimplementationanddatabaseadministrationtopicsthanispossibleinthisbook.2|Chapter1: GettingStartedForthePythonlanguageandstandardlibrary,youcanusethehelp()functionintheinterpreterorthepydoctoolonthecommandlinetogetAPIdocumentationforanymethodsormodules.Forexample:pydocstringThelatestPythonlanguageandAPIdocumentationisalsoavailableforonlinebrowsingathttp://docs.python.org/.10gen’sPyMongodriverhasAPIdocumentationavailableonlinetogowitheachre-lease.Youcanfindthisathttp://api.mongodb.org/python/.Additionally,onceyouhavethePyMongodriverpackageinstalledonyoursystem,asummaryversionoftheAPIdocumentationshouldbeavailabletoyouinthePythoninterpreterviathehelp()function.Duetoanissuewiththevirtualenvtoolmentionedinthenextsection,“py-doc”doesnotworkinsideavirtualenvironment.Youmustinsteadrunpython-mpydocpymongo.InstallingMongoDBForthepurposesofdevelopment,itisrecommendedtorunaMongoDBserveronyourlocalmachine.Thiswillpermityoutoiteratequicklyandtrynewthingswithoutfearofdestroyingaproductiondatabase.Additionally,youwillbeabletodevelopwithMongoDBevenwithoutanInternetconnection.Dependingonyouroperatingsystem,youmayhavemultipleoptionsforhowtoinstallMongoDBlocally.MostmodernUNIX-likesystemswillhaveaversionofMongoDBavailableintheirpackagemanagementsystem.ThisincludesFreeBSD,Debian,Ubuntu,Fedora,Cen-tOSandArchLinux.Installingoneofthesepackagesislikelythemostconvenientap-proach,althoughtheversionofMongoDBprovidedbyyourpackagingvendormaylagbehindthelatestreleasefrom10gen.Forlocaldevelopment,aslongasyouhavethelatestmajorrelease,youareprobablyfine.10genalsoprovidestheirownMongoDBpackagesformanysystemswhichtheyupdateveryquicklyoneachrelease.Thesecanbealittlemoreworktogetinstalledbutensureyouarerunningthelatest-and-greatest.Aftertheinitialsetup,theyaretypicallytrivialtokeepup-to-date.Foraproductiondeployment,whereyoulikelywanttobeabletoupdatetothemostrecentstableMongoDBversionwithaminimumofhassle,thisoptionprobablymakesthemostsense.InadditiontothesystempackageversionsofMongoDB,10genprovidebinaryzipandtararchives.Theseareindependentofyoursystempackagemanagerandareprovidedinboth32-bitand64-bitflavoursforOSX,Windows,LinuxandSolaris.10genalsoprovidestatically-builtbinarydistributionsofthiskindforLinux,whichmaybeyourbestoptionifyouarestuckonanolder,legacyLinuxsystemlackingthemodernlibcInstallingMongoDB|3andotherlibraryversions.Also,ifyouareonOSX,WindowsorSolaris,theseareprobablyyourbestbet.Finally,youcanalwaysbuildyourownbinariesfromthesourcecode.UnlessyouneedtomakemodificationstoMongoDBinternalsyourself,thismethodisbestavoidedduetothetimeandcomplexityinvolved.Intheinterestsofsimplicity,wewillprovidethecommandsrequiredtoinstallastableversionofMongoDBusingthesystempackagemanagerofthemostcommonUNIX-likeoperatingsystems.Thisistheeasiestmethod,assumingyouareononeoftheseplatforms.ForMacOSXandWindows,weprovideinstructionstoinstallthebinarypackagesfrom10gen.Ubuntu/Debian:sudoapt-getupdate;sudoapt-getinstallmongodbFedora:sudoyuminstallmongo-stable-serverFreeBSD:sudopkg_add-rmongodbWindows:Gotohttp://www.mongodb.organddownloadthelatestproductionreleasezipfileforWindows—choosing32-bitor64-bitdependingonyoursystem.ExtractthecontentsofthezipfiletoalocationlikeC:\mongodbandaddthebindirectorytoyourPATH.MacOSX:Gotohttp://www.mongodb.organddownloadthelatestproductionreleasecompressedtarfileforOSX—choosing32-bitor64-bitdependingonyoursystem.Extractthecontentstoalocationlike/usr/local/or/optandaddthebindirectorytoyour$PATH.Forexmaple:cd/tmpwgethttp://fastdl.mongodb.org/osx/mongodb-osx-x86_64-1.8.3-rc1.tgztarxfzmongodb-osx-x86_64-1.8.3-rc1.tgzsudomkdir/usr/local/mongodbsudocp-rmongodb-osx-x86_64-1.8.3-rc1/bin/usr/local/mongodb/exportPATH=$PATH:/usr/local/mongodb/bin4|Chapter1: GettingStartedInstallMongoDBonOSXwithMacPortsIfyouwouldliketotryathird-partysystempackagemanagementsystemonMacOSX,youmayalsoinstallMongoDB(andPython,infact)throughMacPorts.MacPortsissimilartoFreeBSDports,butforOSX.Awordofwarningthough:MacPortscompilesfromsource,andsocantakeconsid-erablylongertoinstallsoftwarecomparedwithsimplygrabbingthebinaries.Futher-more,youwillneedtohaveApple’sXcodeDeveloperToolsinstalled,alongwiththeX11windowingenvironment.ThefirststepistoinstallMacPortsfromhttp://www.macports.org.WerecommenddownloadingandinstallingtheirDMGpackage.OnceyouhaveMacPortsinstalled,youcaninstallMongoDBwiththecommand:sudoportselfupdate;sudoportinstallmongodbToinstallPython2.7fromMacPortsusethecommand:sudoportselfupdate;sudoportinstallpython27RunningMongoDBOnsomeplatforms—suchasUbuntu—thepackagemanagerwillautomaticallystartthemongoddaemonforyou,andensureitstartsonbootalso.Onothers,suchasMacOSX,youmustwriteyourownscripttostartit,andmanuallyintegratewithlaunchdsothatitstartsonsystemboot.NotethatbeforeyoucanstartMongoDB,itsdataandlogdirectoriesmustexist.IfyouwishtohaveMongoDBstartautomaticallyonbootonWindows,10genhaveadocumentdescribinghowtosetthisupathttp://www.mongodb.org/display/DOCS/Windows+ServiceTohaveMongoDBstartautomaticallyonbootunderMacOSX,firstyouwillneedaplistfile.Savethefollowing(changingdbandlogpathsappropriately)to/Library/LaunchDaemons/org.mongodb.mongod.plist:<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEplistPUBLIC"-//Apple//DTDPLIST1.0//EN""http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plistversion="1.0"><dict><key>RunAtLoad</key><true/><key>Label</key><string>org.mongo.mongod</string><key>ProgramArguments</key><array><string>/usr/local/mongodb/bin/mongod</string><string>--dbpath</string>RunningMongoDB|5<string>/usr/local/mongodb/data/</string><string>--logpath</string><string>/usr/local/mongodb/log/mongodb.log</string></array></dict></plist>Nextrunthefollowingcommandstoactivatethestartupscriptwithlaunchd:sudolaunchctlload/Library/LaunchDaemons/org.mongodb.mongod.plistsudolaunchctlstartorg.mongodb.mongodAquickwaytotestwhetherthereisaMongoDBinstancealreadyrunningonyourlocalmachineistotypemongoatthecommand-line.ThiswillstarttheMongoDBadminconsole,whichattemptstoconnecttoadatabaseserverrunningonthedefaultport(27017).Inanycase,youcanalwaysstartMongoDBmanuallyfromthecommand-line.Thisisausefulthingtobefamiliarwithincaseyoueverwanttotestfeaturessuchasreplicasetsorshardingbyrunningmultiplemongodinstancesonyourlocalmachine.Assumingthemongodbinaryisinyour$PATH,run:mongod--logpath<path/to/mongo.logfile>--port<porttolistenon>--dbpath<path/to/datadirectory>SettingupaPythonEnvironmentwithMongoDBInordertobeabletoconnecttoMongoDBwithPython,youneedtoinstallthePy-Mongodriverpackage.InPython,thebestpracticeist

相关资料推荐

  • 名称/格式
  • 下载次数
  • 资料大小

用户评论

0/200
暂无评论
上传我的资料

资料阅读排行

该用户的其它资料

关闭

请选择举报的类型

关闭

提示

提交成功!

感谢您对爱问共享资料的支持,我们将尽快核实并处理您的举报信息。

关闭

提示

提交失败!

您的举报信息提交失败,请重试!

关闭

提示

重复举报!

亲爱的用户!感觉您对爱问共享资料的支持,请勿重复举报噢!

全屏 缩小 放大
收藏
资料评价:

/ 66
所需积分:0 立即下载
返回
顶部
举报
资料
关闭

温馨提示

感谢您对爱问共享资料的支持,精彩活动将尽快为您呈现,敬请期待!