introductiontomongodb.ppt

上传人:仙人指路1688 文档编号:2970550 上传时间:2023-03-06 格式:PPT 页数:36 大小:4.22MB
返回 下载 相关 举报
introductiontomongodb.ppt_第1页
第1页 / 共36页
introductiontomongodb.ppt_第2页
第2页 / 共36页
introductiontomongodb.ppt_第3页
第3页 / 共36页
introductiontomongodb.ppt_第4页
第4页 / 共36页
introductiontomongodb.ppt_第5页
第5页 / 共36页
点击查看更多>>
资源描述

《introductiontomongodb.ppt》由会员分享,可在线阅读,更多相关《introductiontomongodb.ppt(36页珍藏版)》请在三一办公上搜索。

1、Chris K,Intro to MongoDB,In This Talk,What is MongoDB?Why use it?Documents and CollectionsQueryingJavaScript ShellSchema Design,What is MongoDB?,Not a RDBMS,Mongo is not a relational database like MySQLNo transactionsNo referential integrityNo joinsNo schema,so no columns or rowsNoSQL,Not a Key-Valu

2、e Store,Mongo is not simply a key-value store like RedisStores structured dataRich query interfaceIndexesMap/ReduceAutomatic sharding,GridFS,geospatial indexing,etc.,Document-oriented Database,Records are JSON documents(actually BSON)Stored in collectionsNo predefined schemaDocs in the same collecti

3、on dont even need to have the same fieldsAtomic in-place operators for contention-free updates$set,$inc,$push,$pop,etc.,Mongo Document,user=name:Frank Furter,occupation:A scientist,location:Transylvania,Why Use MongoDB?,Its Stupid Fast!,Anywhere from 2 to 10 times faster than MySQLDepends on which c

4、ontrived benchmark youre looking atHeres one I just made up:,Its Stupid Fast!,About 50 times faster than CouchDBAccording to http:/important points:Its pretty quickBenchmarks are worthless unless you do them on your actual workload,Its Web Scale!,Sharding built-in,automatic,and*Just Works*Just Works

5、 guarantee applies only if you have a cluster of shard replica sets with config servers and routing servers and you define your own shard key(s)with appropriate uniformity and granularityAsynchronous replication for failover and redundancy,Its Pretty Painless,SchemalessNo more configuring database c

6、olumns with typesNo more defining and managing migrationsJust stick your data in there,its fineNoSQLORMs exist mostly because writing SQL sucksMongos query language is basically JSONThe Mongo driver for your favorite language is really nice and officially supportedHandy JavaScript shell for the CLI,

7、Its Pretty Painless,/*First go create the database,the table,the schema,etc.*/mysql_connect(localhost,username,password)or die(mysql_error();mysql_select_db(test)or die(mysql_error();$sql=INSERT INTO users(name,age)VALUES(Janet,23);mysql_query($sql);$result=mysql_query(SELECT*FROM users WHERE age=23

8、);$row=mysql_fetch_assoc($result);echo Oh,.$rowname.!;/prints Oh,Janet!,$mongo=new Mongo();/defaults to localhost with no auth$users=$mongo-test_db-users;/database and collection created implicitly$users-insert(array(name=Brad,age=25);$user=$users-findOne(array(age=25);echo Oh,.$user-name.!;/prints

9、Oh,Brad!,MySQL,MongoDB,All the Cool Kids Are Doing It,http:/www.mongodb.org/display/DOCS/Production+Deployments,Documents and Collections,Documents and Collections,Documents are the recordsLike objects in OOP,or rows in RDBMSCollections are groups of documentsUsually represent a top-level class in y

10、our appHeterogeneous setUnlike RDBMS tables,no predefined schemaNo foreign keys,so how do we reference other objects?Dont!Just embed the sub-item in the parent docOr,use a key for references and deal with the fact that you dont get integrity or joins,Embedded Objects,Documents can embed other docume

11、ntsUsed to efficiently represent a relationFor example:,name:Brad Majors,address:street:Oak Terrace,city:Denton,Querying,Queries,Queries are documentsQuery expression objects indicate a pattern to matchdb.users.find(last_name:Smith)Several query objects for advanced queriesdb.users.find(age:$gte:23)

12、db.users.find(age:$in:23,25),Querying Embedded Objects,Exact match an entire embedded objectdb.users.find(address:street:Oak Terrace,city:Denton)Dot-notation for a partial matchdb.users.find(address.city:Denton),JavaScript Shell,JS Shell,Comes with MongoDBLaunch it with mongo on the command-lineTry

13、a simplified version at http:/www.mongodb.org/Great fit since Mongo docs are basically JSON,Live Demo,If the tech demo gods allow it,Schema Design,I thought you said no schema?,There is no predefined schemaYour application creates an ad-hoc schema with the objects it createsThe schema is implicit in

14、 the queries your application runs,Schema Design,Use collections to represent the top-level classes of your applicationBut dont just make a collection for every object typeThese arent like tables in an RDBMSLess normalization,more embedding,Obligatory Blog Post Example,A blog post has an author,some

15、 text,and many commentsThe comments are unique per post,but one author has many postsHow would you design this in SQL?Lets look at how we might design it in Mongo,Bad Schema Design:References,Collections for posts,authors,and commentsReferences by manually created ID,post=id:150,author:100,text:This

16、 is a pretty awesome post.,comments:100,105,112author=id:100,name:Michael Arrington posts:150comment=id:105,text:Whatever this sux.,Better Schema Design:Embedding,Collection for postsEmbed comments,author name,post=author:Michael Arrington,text:This is a pretty awesome post.,comments:Whatever this p

17、ost sux.,I agree,lame!,Benefits,Embedded objects brought back in the same query as parent objectOnly 1 trip to the DB server requiredObjects in the same collection are generally stored contiguously on diskSpatial locality=fasterIf the document model matches your domain well,it can be much easier to

18、comprehend than nasty joins,Indexes,Mongo supports indexes to greatly improve query performanceNo need to create in advanceCreate idempotent indexes in your app with ensure_index,Schema Design Limitations,No referential integrityHigh degree of denormalization means updating something in many places

19、instead of oneLack of predefined schema is a double-edged swordHopefully you have a model in your appObjects within a collection can be completely inconsistent in their fields,Final Thoughts,Final Thoughts,MongoDB is fast no matter how you slice itIt achieves high performance by literally playing fa

20、st and loose with your dataThats not necessarily a bad thing,just a tradeoffVery rapid development,open sourceDocument model is simple but powerfulAdvanced features like map/reduce,geospatial indexing etc.are very compellingSurprisingly great drivers for most languages,Questions?,Thanks!,These slides are online:http:/bit.ly/intro_to_mongo,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号