What is NoSQL? NoSQL Databases Explained | MongoDB

文章推薦指數: 80 %
投票人數:10人

NoSQL databases (aka "not only SQL") are non-tabular databases and store data differently than relational tables. NoSQL databases come in a variety of types ... NoSQLdatabases(aka"notonlySQL")arenon-tabulardatabasesandstoredatadifferentlythanrelationaltables.NoSQLdatabasescomeinavarietyoftypesbasedontheirdatamodel.Themaintypesaredocument,key-value,wide-column,andgraph.Theyprovideflexibleschemasandscaleeasilywithlargeamountsofdataandhighuserloads.Inthisarticle,you'lllearnwhataNoSQLdatabaseis,why(andwhen!)youshoulduseone,andhowtogetstarted.OverviewThisarticlewillcover:WhatisaNoSQLDatabase? BriefHistoryofNoSQLDatabasesNoSQLDatabaseFeaturesTypesofNoSQLDatabaseDifferencebetweenRDBMSandNoSQLWhyNoSQL?WhenshouldNoSQLbeUsed?NoSQLDatabaseMisconceptionsNoSQLQueryTutorialSummaryFAQWhatisaNoSQLdatabase?Whenpeopleusetheterm“NoSQLdatabase,”theytypicallyuseittorefertoanynon-relationaldatabase.Somesaytheterm“NoSQL”standsfor“nonSQL”whileotherssayitstandsfor“notonlySQL.”Eitherway,mostagreethatNoSQLdatabasesaredatabasesthatstoredatainaformatotherthanrelationaltables.BriefhistoryofNoSQLdatabasesNoSQLdatabasesemergedinthelate2000sasthecostofstoragedramaticallydecreased.Gonewerethedaysofneedingtocreateacomplex,difficult-to-managedatamodelinordertoavoiddataduplication.Developers(ratherthanstorage)werebecomingtheprimarycostofsoftwaredevelopment,soNoSQLdatabasesoptimizedfordeveloperproductivity.Asstoragecostsrapidlydecreased,theamountofdatathatapplicationsneededtostoreandqueryincreased.Thisdatacameinallshapesandsizes—structured,semi-structured,andpolymorphic—anddefiningtheschemainadvancebecamenearlyimpossible.NoSQLdatabasesallowdeveloperstostorehugeamountsofunstructureddata,givingthemalotofflexibility.Additionally,theAgileManifestowasrisinginpopularity,andsoftwareengineerswererethinkingthewaytheydevelopedsoftware.Theywererecognizingtheneedtorapidlyadapttochangingrequirements.Theyneededtheabilitytoiteratequicklyandmakechangesthroughouttheirsoftwarestack—allthewaydowntothedatabase.NoSQLdatabasesgavethemthisflexibility.Cloudcomputingalsoroseinpopularity,anddevelopersbeganusingpubliccloudstohosttheirapplicationsanddata.Theywantedtheabilitytodistributedataacrossmultipleserversandregionstomaketheirapplicationsresilient,toscaleoutinsteadofscaleup,andtointelligentlygeo-placetheirdata.SomeNoSQLdatabaseslikeMongoDBprovidethesecapabilities.NoSQLdatabasefeaturesEachNoSQLdatabasehasitsownuniquefeatures.Atahighlevel,manyNoSQLdatabaseshavethefollowingfeatures:FlexibleschemasHorizontalscalingFastqueriesduetothedatamodelEaseofusefordevelopersCheckoutWhataretheBenefitsofNoSQLDatabases?tolearnmoreabouteachofthefeatureslistedabove.TypesofNoSQLdatabasesOvertime,fourmajortypesofNoSQLdatabasesemerged:documentdatabases,key-valuedatabases,wide-columnstores,andgraphdatabases.DocumentdatabasesstoredataindocumentssimilartoJSON(JavaScriptObjectNotation)objects.Eachdocumentcontainspairsoffieldsandvalues.Thevaluescantypicallybeavarietyoftypesincludingthingslikestrings,numbers,booleans,arrays,orobjects.Key-valuedatabasesareasimplertypeofdatabasewhereeachitemcontainskeysandvalues.Wide-columnstoresstoredataintables,rows,anddynamiccolumns.Graphdatabasesstoredatainnodesandedges.Nodestypicallystoreinformationaboutpeople,places,andthings,whileedgesstoreinformationabouttherelationshipsbetweenthenodes.Tolearnmore,visitUnderstandingtheDifferentTypesofNoSQLDatabases.DifferencebetweenRDBMSandNoSQLdatabasesWhileavarietyofdifferencesexistbetweenrelationaldatabasemanagementsystems(RDBMS)andNoSQLdatabases,oneofthekeydifferencesisthewaythedataismodeledinthedatabase.Inthissection,we'llworkthroughanexampleofmodelingthesamedatainarelationaldatabaseandaNoSQLdatabase.Then,we'llhighlightsomeoftheotherkeydifferencesbetweenrelationaldatabasesandNoSQLdatabases.RDBMSvsNoSQL:DataModelingExampleLet'sconsideranexampleofstoringinformationaboutauserandtheirhobbies.Weneedtostoreauser'sfirstname,lastname,cellphonenumber,city,andhobbies.Inarelationaldatabase,we'dlikelycreatetwotables:oneforUsersandoneforHobbies.UsersIDfirst_namelast_namecellcity1LeslieYepp8125552344PawneeHobbiesIDuser_idhobby101scrapbooking111eatingwaffles121workingInordertoretrievealloftheinformationaboutauserandtheirhobbies,informationfromtheUserstableandHobbiestablewillneedtobejoinedtogether.ThedatamodelwedesignforaNoSQLdatabasewilldependonthetypeofNoSQLdatabasewechoose.Let'sconsiderhowtostorethesameinformationaboutauserandtheirhobbiesinadocumentdatabaselikeMongoDB.{ "_id":1, "first_name":"Leslie", "last_name":"Yepp", "cell":"8125552344", "city":"Pawnee", "hobbies":["scrapbooking","eatingwaffles","working"] }Inordertoretrievealloftheinformationaboutauserandtheirhobbies,asingledocumentcanberetrievedfromthedatabase.Nojoinsarerequired,resultinginfasterqueries.Toseeamoredetailedversionofthisdatamodelingexample,readMappingTermsandConceptsfromSQLtoMongoDB.OtherdifferencesbetweenRDBMSandrelationaldatabasesWhiletheexampleabovehighlightsthedifferencesindatamodelsbetweenrelationaldatabasesandNoSQLdatabases,manyotherimportantdifferencesexist,including:FlexibilityoftheschemaScalingtechniqueSupportfortransactionsRelianceondatatoobjectmappingTolearnmoreaboutthedifferencesbetweenrelationaldatabasesandNoSQLdatabases,visitNoSQLvsSQLDatabases.WhyNoSQL?NoSQLdatabasesareusedinnearlyeveryindustry.Usecasesrangefromthehighlycritical(e.g.,storingfinancialdataandhealthcarerecords)tothemorefunandfrivolous(e.g.,storingIoTreadingsfromasmartkittylitterbox).Inthefollowingsections,we'llexplorewhenyoushouldchoosetouseaNoSQLdatabaseandcommonmisconceptionsaboutNoSQLdatabases.WhenshouldNoSQLbeused?Whendecidingwhichdatabasetouse,decision-makerstypicallyfindoneormoreofthefollowingfactorsleadthemtoselectingaNoSQLdatabase:Fast-pacedAgiledevelopmentStorageofstructuredandsemi-structureddataHugevolumesofdataRequirementsforscale-outarchitectureModernapplicationparadigmslikemicroservicesandreal-timestreamingSeeWhentoUseNoSQLDatabasesandExploringNoSQLDatabaseExamplesformoredetailedinformationonthereasonslistedabove.NoSQLdatabasemisconceptionsOvertheyears,manymisconceptionsaboutNoSQLdatabaseshavespreadthroughoutthedevelopercommunity.Inthissection,we'lldiscusstwoofthemostcommonmisconceptions:Relationshipdataisbestsuitedforrelationaldatabases.NoSQLdatabasesdon'tsupportACIDtransactions.Tolearnmoreaboutcommonmisconceptions,readEverythingYouKnowAboutMongoDBisWrong.Misconception:relationshipdataisbestsuitedforrelationaldatabasesAcommonmisconceptionisthatNoSQLdatabasesornon-relationaldatabasesdon’tstorerelationshipdatawell.NoSQLdatabasescanstorerelationshipdata—theyjuststoreitdifferentlythanrelationaldatabasesdo.Infact,whencomparedwithrelationaldatabases,manyfindmodelingrelationshipdatainNoSQLdatabasestobeeasierthaninrelationaldatabases,becauserelateddatadoesn’thavetobesplitbetweentables.NoSQLdatamodelsallowrelateddatatobenestedwithinasingledatastructure.Misconception:NoSQLdatabasesdon'tsupportACIDtransactionsAnothercommonmisconceptionisthatNoSQLdatabasesdon'tsupportACIDtransactions.SomeNoSQLdatabaseslikeMongoDBdo,infact,supportACIDtransactions.NotethatthewaydataismodeledinNoSQLdatabasescaneliminatetheneedformulti-recordtransactionsinmanyusecases.Considertheearlierexamplewherewestoredinformationaboutauserandtheirhobbiesinbotharelationaldatabaseandadocumentdatabase.Inordertoensureinformationaboutauserandtheirhobbieswasupdatedtogetherinarelationaldatabase,we'dneedtouseatransactiontoupdaterecordsintwotables.Inordertodothesameinadocumentdatabase,wecouldupdateasingledocument—nomulti-recordtransactionrequired.NoSQLquerytutorialAvarietyofNoSQLdatabasesexist.Today,we'llbetryingMongoDB,theworld'smostpopularNoSQLdatabaseaccordingtoDB-Engines.Inthistutorial,you'llloadasampledatabaseandlearntoqueryit—allwithoutinstallinganythingonyourcomputerorpayinganything.AuthenticatetoMongoDBAtlasTheeasiestwaytogetstartedwithMongoDBisMongoDBAtlas.AtlasisMongoDB'sfullymanageddatabase-as-a-service.Atlashasaforeverfreetier,whichiswhatyou'llbeusingtoday.NavigatetoAtlas.Createanaccountifyouhaven'talready.LogintoAtlas.CreateanAtlasorganizationandproject.Formoreinformationonhowtocompletethestepsabove,visittheofficialMongoDBdocumentationoncreatinganAtlasaccount.CreateaclusterandadatabaseAclusterisaplacewhereyoucanstoreyourMongoDBdatabases.Inthissection,you'llcreateafreecluster.Onceyouhaveacluster,youcanbeginstoringdatainAtlas.YoucouldchoosetomanuallycreateadatabaseintheAtlasDataExplorer,intheMongoDBShell,inMongoDBCompass,orusingyourfavoriteprogramminglanguage.Instead,inthisexample,youwillimportAtlas'ssampledataset.CreateafreeclusterbyfollowingthestepsintheofficialMongoDBdocumentation.LoadthesampledatasetbyfollowingtheinstructionsintheofficialMongoDBdocumentation.Loadingthesampledatasetwilltakeseveralminutes.Whilewedon'tneedtothinkaboutdatabasedesignforthistutorial,notethatdatabasedesignanddatamodelingaremajorfactorsinMongoDBperformance.LearnmoreaboutbestpracticesformodelingdatainMongoDB:MongoDBSchemaDesignPatternsBlogSeriesMongoDBSchemaDesignAnti-PatternsBlogSeriesFreeMongoDBUniversityCourse:M320DataModelingQuerythedatabaseNowthatyouhavedatainyourcluster,let'squeryit!Justlikeyouhadmultiplewaystocreateadatabase,youhavemultipleoptionsforqueryingadatabase:intheAtlasDataExplorer,intheMongoDBShell,inMongoDBCompass,orusingyourfavoriteprogramminglanguage.Inthissection,you’llquerythedatabaseusingtheAtlasDataExplorer.Thisisagoodwaytogetstartedquerying,asitrequireszerosetup.NavigatetotheDataExplorer(theCollectionstab),ifyouarenotalreadythere.SeetheofficialMongoDBdocumentationforinformationonhowtonavigatetotheDataExplorer.TheleftpaneloftheDataExplorerdisplaysalistofdatabasesandcollectionsinthecurrentcluster.TherightpaneloftheDataExplorerdisplaysalistofdocumentsinthecurrentcollection.TheDataExplorerdisplaysalistofdocumentsinthelistingsAndReviewscollection.Expandthesample_mflixdatabaseintheleftpanel.Alistofthedatabase'scollectionsisdisplayed.Selectthemoviescollection.TheFindViewisdisplayedintherightpanel.Thefirsttwentydocumentsoftheresultsaredisplayed.Youarenowreadytoquerythemoviescollection.Let'squeryforthemoviePrideandPrejudice.Inthequerybar,input{title:"PrideandPrejudice"}inthequerybarandclickApply.Twodocumentswiththetitle“PrideandPrejudice”arereturned. Theresultsforqueryingformovieswiththetitle"PrideandPrejudice".Congrats!You'vesuccessfullyqueriedaNoSQLdatabase!ContinueexploringyourdataInthistutorial,weonlyscratchedthesurfaceofwhatyoucandoinMongoDBandAtlas.ContinueinteractingwithyourdatabyusingtheDataExplorertoinsertnewdocuments,editexistingdocuments,anddeletedocuments.Whenyouarereadytotrymoreadvancedqueriesthataggregateyourdata,createanaggregationpipeline.Theaggregationframeworkisanincrediblypowerfultoolforanalyzingyourdata.Tolearnmore,takethefreeMongoDBUniversityCourseM121TheMongoDBAggregationFramework.Whenyouwanttovisualizeyourdata,checkoutMongoDBCharts.ChartsistheeasiestwaytovisualizedatastoredinAtlasandAtlasDataLake.Chartsallowsyoutocreatedashboardsthatarefilledwithvisualizationsofyourdata.SummaryNoSQLdatabasesprovideavarietyofbenefitsincludingflexibledatamodels,horizontalscaling,lightningfastqueries,andeaseofusefordevelopers.NoSQLdatabasescomeinavarietyoftypesincludingdocumentdatabases,key-valuesdatabases,wide-columnstores,andgraphdatabases.MongoDBistheworld'smostpopularNoSQLdatabase.LearnmoreaboutMongoDBAtlas,andgivethefreetieratry.ExcitedtolearnmorenowthatyouhaveyourownAtlasaccount?HeadovertoMongoDBUniversitywhereyoucangetfreeonlinetrainingfromMongoDBengineersandearnaMongoDBcertification.TheQuickStartTutorialsareanothergreatplacetobegin;theywillgetyouupandrunningquicklywithyourfavoriteprogramminglanguage.FollowthistutorialwithMongoDBAtlasExperiencethebenefitsofusingMongoDB,thepremierNoSQLdatabase,onthecloud.GetStartedFree!FAQWhataretheadvantagesofNoSQL?ManyNoSQLdatabaseshavethefollowingadvantages:FlexibleschemasHorizontalscalingFastqueriesduetothedatamodelEaseofusefordevelopersCheckoutWhataretheBenefitsofNoSQLDatabases?formoredetails.Whatiseventualconsistency?Eventualconsistencyisapropertyofdistributeddatabases.Eventualconsistencyensuresthatwhenanupdateismadetothedatabase,eventuallyallnodesinthedistributeddatabasewillreflectthatupdate.WhatistheCAPtheorem?TheCAPtheoremstatesthatadistributedcomputingsystemcanprovideamaximumoftwoofthefollowingthreeproperties:consistency,availability,andpartitiontolerance.WhatisNoSQLusedfor?NoSQLdatabasesareusedinnearlyeveryindustryforavarietyofusecases.ThetypeofNoSQLdatabasedeterminesthetypicalusecase.Forexample,documentdatabaseslikeMongoDBaregeneralpurposedatabases.Key-valuedatabasesareidealforlargevolumesofdatawithsimplelookupqueries.Wide-columnstoresworkwellforusecaseswithlargeamountsofdataandpredictablequerypatterns.Graphdatabasesexcelatanalyzingandtraversingrelationshipsbetweendata.SeeUnderstandingtheDifferentTypesofNoSQLDatabasesformoreinformation.WhatisaNoSQLdatabase?ANoSQLdatabaseisadatabasethatstoresdatainaformatotherthanrelationaltables.HowdoIwriteaNoSQLquery?EachNoSQLdatabasewillhaveitsownapproachtowritingqueries.VisittheinteractiveMongoDBdocumentationtolearnmoreaboutqueryingaMongoDBdatabase.IsNoSQLhardtolearn?No,NoSQLdatabasesarenothardtolearn.Infact,manydevelopersfindmodelingdatainNoSQLdatabasestobeincrediblyintuitive.Forexample,documentsinMongoDBmaptodatastructuresinmostpopularprogramminglanguages,makingprogrammingfasterandeasier.NotethatthosewithtrainingandexperienceinrelationaldatabaseswilllikelyfaceabitofalearningcurveastheyadjusttonewwaysofmodelingdatainNoSQLdatabases.IsJSONaNoSQL?DocumentdatabasesareatypeofNoSQLdatabasethatstoredatainJSONorBSONdocuments.WhatlanguageisusedtoqueryNoSQL?NoSQLdatabasesspanavarietyoftypesandimplementations.Asaresult,NoSQLdatabasescanbequeriedusingavarietyofquerylanguagesandAPIs.MongoDB,theworld'smostpopularNoSQLdatabase,canbequeriedusingtheMongoDBQueryLanguage(MQL).DoesNoSQLhaveschema?NoSQLdatabasestypicallyhaveflexibleschemas.NotethatsomeNoSQLdatabaseslikeMongoDBalsohavesupportforschemavalidation,sodeveloperscanlockdowntheirschemasasmuchoraslittleasthey'dlikewhentheyareready.ThisarticlewaswrittenbyLaurenSchaefer,aMongoDBDeveloperAdvocate.LearnmoreaboutkeydifferencesbetweenNoSQLvsSQLDatabasesRelatedNoSQLResourcesWhatarethemaindifferencesbetweenNoSQLandSQL?WhenshouldyouuseaNoSQLdatabase?Whatarethe4differenttypesofNoSQLdatabases?NoSQLDatabasesAdvantagesNoSQLdatamodelingandschemadesignNoSQLDatabaseExamples



請為這篇文章評分?