高级数据库课件:09-MongoDB.pptx
- 【下载声明】
1. 本站全部试题类文档,若标题没写含答案,则无答案;标题注明含答案的文档,主观题也可能无答案。请谨慎下单,一旦售出,不予退换。
2. 本站全部PPT文档均不含视频和音频,PPT中出现的音频或视频标识(或文字)仅表示流程,实际无音频或视频文件。请谨慎下单,一旦售出,不予退换。
3. 本页资料《高级数据库课件:09-MongoDB.pptx》由用户(罗嗣辉)主动上传,其收益全归该用户。163文库仅提供信息存储空间,仅对该用户上传内容的表现方式做保护处理,对上传内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知163文库(点击联系客服),我们立即给予删除!
4. 请根据预览情况,自愿下载本文。本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
5. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007及以上版本和PDF阅读器,压缩文件请下载最新的WinRAR软件解压。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 高级 数据库 课件 09 MongoDB
- 资源描述:
-
1、open-source, high-performance, document-oriented database1 OVERVIEWConceptsWho provides MongoDB in the cloud?http:/ is using MongoDB?http:/ DBMSDatabase ModelScore1OracleRelational DBMS1466.362MySQLRelational DBMS1278.363Microsoft SQL ServerRelational DBMS1118.054PostgreSQLRelational DBMS280.95Mongo
2、DB Document store279.056DB2Relational DBMS198.77Microsoft AccessRelational DBMS146.498Cassandra Wide column store108.919SQLiteRelational DBMS107.9710RedisKey-value store95.49200k+Education RegistrationsConceptsCommunity30k+MongoDB User Group Members8 Million +DownloadsConceptsDrivershttp:/docs.mongo
3、db.org/ecosystem/drivers/MongoDBDrivernCnC+nC#nJavanNode.jsnPerlnPHPnPythonnRubynScalaAppHorizontally ScalableArchitecturesno joinsno complex transactions+1.1 基本概念JSON-style Documents hello: “world” x16x00 x00 x00 x02hellox00 x06x00 x00 x00worldx00 x00http:/bsonspec.orgrepresented as BSONJust like a
4、 light and friendly XMLFlexible “Schemas”In collection db.posts:author: “mike”, links: 3, date: Sun Jul 18 2010 14:40:20 GMT-0700 (PDT) text: “blah blah”author: “eliot”, date: Sun Jul 18 2010 14:40:22 GMT-0700 (PDT) text: “Here is MongoDB .”, views: 10Potentially all documents in the same collection
5、Embedded Document _id: ObjectId(4d1009c7262bb4b94af1cea4) author_id: “1346”, date: Sun Jul 18 2010 14:40:20 GMT-0700 (PDT), title: “my story” text: “once upon a time .”, tags: “novel”,”english”, Comments: user_id: 234, text: “awesome dude”, user_id: 1235, text: “that made me cry”little need for join
6、sor transactions across documents!FeaturesComplex queryingAtomic updates with modifiersIndexing (unique, compound, Geo)Aggregation and Map / ReduceCapped CollectionsPowerful Shell (Javascript)GridFS: file storageMongoDB Pluggable Storage EnginesReplicationmasterslaveslaveUsing Replica Set:- pool of
7、servers with 1 master- automatic master election and failover- distributed reads (slaveOk)slaveClientClientShardingclientmongos.mongosmongod.ShardsmongodmongodmongodConfigServersmongodmongodmongodmongodmongodmongodmongodmongod.For large datasets, or write heavy systemSupportOS: Mac OS X, Windows, Li
8、nux, Solaris, 32/64 bitsDrivers: C, C#, C+, Haskell, Java, Javascript, Perl, PHP, Python, Ruby, Scala. + community driversOpen-source project with active community, Wiki, Google Group, 10gen consulting / supportProduction Examples Shutterfly Fourquare Craigslist bit.ly IGN Sourceforge Etsy the New Y
9、ork Times Business Insider Gilt Groupe Intuit College humor Evite Disqus Justin.tv Heartbeat Hot Potato Eventbrite Sugar crm Electronic Arts .New Post post = author: mike,. date: new Date(),. text: my blog post,. tags: mongodb, intro db.posts.save(post) db.posts.findOne() _id : ObjectId(4d2f944103e8
10、fdbb36f6d205),author : mike,date : ISODate(2011-01-14T00:08:49.933Z),text : my blog post,tags : mongodb,introA Quick Asidespecial keypresent in all documentsunique across a Collectionany type you want_id1.2 基本操作Update db.posts.update(_id: post._id,. $set: author: tony) c = author: eliot, date: new D
11、ate(), text: great post! db.posts.update(_id: post._id,. $push: comments: c) db.posts.update(_id: post._id,. $inc: views: 1)Update db.posts.update(_id: post._id,. $set: author: tony) c = author: eliot, date: new Date(), text: great post! db.posts.update(_id: post._id,. $push: comments: c) db.posts.u
12、pdate(_id: post._id,. $inc: views: 1)Querying db.posts.findOne()_id : ObjectId(4d2f944103e8fdbb36f6d205),author : tony,comments : author : eliot,date : ISODate(2011-01-14T00:13:52.463Z),text : great post!,date : ISODate(2011-01-14T00:08:49.933Z),tags : mongodb,intro,text : my blog post,views : 1More
13、 QueryingFind by Author db.posts.find(author: tony)10 most recent posts: db.posts.find().sort(date: -1).limit(10)Posts since April 1st: april_1 = new Date(2010, 3, 1) db.posts.find(date: $gt: april_1)Adding an index to speed up: db.posts.ensureIndex(author: 1) db.posts.ensureIndex(date: 1)More Query
14、ingFind with regexp: db.posts.find(text: /post$/)Find within array: db.posts.find(tags: intro) db.posts.ensureIndex(tags: 1)Find within embedded object: db.posts.find(comments.author: eliot) db.posts.ensureIndex(comments.author: 1)More QueryingCounting: db.posts.find().count() db.posts.find(author:
15、tony).count()Paging: page = 2 page_size = 15 db.post.find().limit(page_size).skip(page * page_size)Advanced operators:$gt, $lt, $gte, $lte, $ne, $all, $in, $nin, $where db.posts.find($where: this.author = tony | this.title = foo)A data structure that can be used to make certain queries more efficien
16、t.Index on a: 1a: 0, b: 9a: 2, b: 0a: 3, b: 2a: 3, b: 5a: 3, b: 7a: 7, b: 1a: 9, b: 1Index on a: 1, b: -1a: 0, b: 9a: 2, b: 0a: 3, b: 7a: 3, b: 5a: 3, b: 2a: 7, b: 1a: 9, b: 1B-tree StructureQuery for a: 7One index per query *One range operator per query ($)Range operator must be last field in index
17、Using RAM wellmongostat for MongoDB Behaviortail the logs for current optionsiostat for disk utiltop c for CPU usageReferences to your documents, efficiently ordered by keyMaintained in a tree structure, allowing fast lookupx:1y:1x:0.5,y:0.5x:2,y:0.5x:5,y:2x:-4,y:10 x:3,y:fdb.c.findOne( _id:2 ), usi
18、ng index _id:1db.c.find( x:2 ), using index x:1db.c.find( x:$in:2,3 ), using index x:1db.c.find( x.a:1 ), using index x.a:1Matches _id:1,x:a:1db.c.find( x:a:1 ), using index x:1Matches _id:1,x:a:1, but not _id:2,x:a:1,b:2QUESTION: What about db.c.find( $where:“this.x = this.y” ), using index x:1?Ind
19、exes cannot be used for $where type queries, but if there are non-where elements in the query then indexes can be used for the non-where elements.db.c.find( x:$gt:2 ), using index x:1db.c.find( x:$gt:2,$lt:5 ), using index x:1db.c.find( x:/a/ ), using index x:1QUESTION: What about db.c.find( x:/a/ )
20、, using index x:1?The letter a can appear anywhere in a matching string, so lexicographic ordering on strings wont help. However, we can use the index to find the range of documents where x is string (eg not a number) or x is the regular expression /a/.db.c.count( x:2 ) using index x:1db.c.distinct(
21、 x:2 ) using index x:1db.c.update( x:2, x:3 ) using index x:1db.c.remove( x:2 ) using index x:1QUESTION: What about db.c.update( x:2, $inc:x:3 ), using index x:1?Older versions of mongoDB didnt support modifiers on indexed fields, but we now support this.db.c.find( ).sort( x:1 ), using index x:1db.c
22、.find( ).sort( x:-1 ), using index x:1db.c.find( x:$gt:4 ).sort( x:-1 ), using index x:1db.c.find( ).sort( x.a:1 ), using index x.a:1QUESTION: What about db.c.find( y:1 ).sort( x:1 ), using index x:1?The index will be used to ensure ordering, provided there is no better index.db.c.find( x:null ), us
23、ing index x:1Matches _id:5db.c.find( x:$exists:false ), using index x:1Matches _id:5, but not _id:6,x:nullQUESTION: What about db.c.find( x:$exists:true ), using index x:1?The index is not currently used, though we may use the index in a future version of mongoDB.All the following match _id:6,x:2,10
24、 and use index x:1db.c.find( x:2 )db.c.find( x:10 )db.c.find( x:$gt:5 )db.c.find( x:2,10 )db.c.find( x:$in:2,5 )QUESTION: What about db.c.find( x:$all:2,10 )?The index will be used to look up all documents matching x:2.db.c.find( x:10,y:20 ), using index x:1,y:1db.c.find( x:10,y:20 ), using index x:
25、1,y:-1db.c.find( x:$in:10,20,y:20 ), using index x:1,y:1db.c.find().sort( x:1,y:1 ), using index x:1,y:1db.c.find().sort( x:-1,y:1 ), using index x:1,y:-1db.c.find( x:10 ).sort( y:1 ), using index x:1,y:1QUESTION: What about db.c.find( y:10 ).sort( x:1 ), using index x:1,y:1?The index will be used t
展开阅读全文