狠狠撸

狠狠撸Share a Scribd company logo
分享人:酷酷 http://weibo.com/lidaohang
Why  MongoDB  Is Awesome
?
NoSQL 四大类 1.key-value 存储 Examples Tokyo Cabinet/Tyrant, Redis, Voldemort, Oracle BDB 典型应用场景 内容缓存,主要用于处理大量数据的高访问负载,也用于一些日志系统等等。 数据模型 Key  指向  Value  的键值对,通常用 hash table 来实现 强项 查找速度快 弱项 数据无结构化,通常只被当作字符串或者二进制数据
NoSQL 四大类 2. 列式数据库 Examples Cassandra, HBase, Riak 典型应用场景 分布式的文件系统 数据模型 以列簇式存储,将同一列数据存在一起 强项 查找速度快,可扩展性强,更容易进行分布式扩展 弱项 功能相对局限
3. 文档型数据库 NoSQL 四大类 Examples CouchDB, MongoDb 典型应用场景 Web 应用(与 Key-Value 类似, Value 是结构化的,不同的是数据库能够了解 Value 的内容) 数据模型 Key-Value 对应的键值对, Value 为结构化数据 强项 数据结构要求不严格,表结构可变,不需要像关系型数据库一样需要预先定义表结构 弱项 查询性能不高,而且缺乏统一的查询语法。
4. 图结构数据库 NoSQL 四大类 Examples Neo4J, InfoGrid, Infinite Graph 典型应用场景 社交网络,推荐系统等。专注于构建关系图谱 数据模型 图结构 强项 利用图结构相关算法。比如最短路径寻址, N 度关系查找等 弱项 很多时候需要对整个图做计算才能得出需要的信息,而且这种结构不太好做分布式的集群方案。
MongoDB 介绍  谁在使用 ?
MongoDB 介绍  Mongo  是一个高性能,开源,无模式的文档型数据库,它在许多场景下可用于替代传统的关系型数据库或键 / 值存储方式。 Mongo 使用 C++ 开发。
MongoDB 介绍  特点 高性能、易部署、易使用,存储数据非常方便。主要功能特性有: 面向集合存储,易存储对象类型的数据。  模式自由。 支持动态查询。  支持完全索引,包含内部对象。  支持查询。  支持复制和故障恢复。  使用高效的二进制数据存储,包括大型对象(如视频等)。 自动处理碎片,以支持云计算层次的扩展性 支持 Python , PHP , Ruby , Java , C , C# , Javascript , Perl 及 C++ 语言的驱动程序 ,  社区中也提供了对 Erlang  及 .NET 等平台的驱动程序。 文件存储格式为 BSON (一种 JSON 的扩展) 可通过网络访问
MongoDB 介绍  适用场合 网站数据: Mongo 非常适合实时的插入,更新与查询,并具备网站实时数据存储所需的复制及高度伸缩性。 缓存:由于性能很高, Mongo 也适合作为信息基础设施的缓存层。在系统重启之后,由 Mongo 搭建的持久化缓存层可以避免下层的数据源 过载。 大尺寸,低价值的数据:使用传统的关系型数据库存储一些数据时可能会比较昂贵,在此之前,很多时候程序员往往会选择传统的文件进行存储。 高伸缩性的场景: Mongo 非常适合由数十或数百台服务器组成的数据库。 Mongo 的路线图中已经包含对 MapReduce 引擎的内置支持。 用于对象及 JSON 数据的存储: Mongo 的 BSON 数据格式非常适合文档化格式的存储及查询。
MongoDB 介绍  不适用场合 1. 高度事务性的系统:例如银行或会计系统。传统的关系型数据库目前还是更适用于需要大量原子性复杂事务的应用程序。 2. 传统的商业智能应用:针对特定问题的 BI 数据库会对产生高度优化的查询方式。对于此类应用,数据仓库可能是更合适的选择。
MongoDB 操作  安装 wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-1.2.4.tgz tar -zxvf mongodb-linux-x86_64-1.2.4.tgz mkdir -p /data/db cd /data/db mkdir m_data m_log cd mongodb-linux-x86_64-1.2.4 启动: bin/mongod --dbpath=/data/db/m_data --logpath=/data/db/m_log  --logappend --port=27017 &
惭辞苍驳辞快速入门
惭辞苍驳辞快速入门
惭辞苍驳辞快速入门
惭辞苍驳辞快速入门
惭辞苍驳辞快速入门
MongoDB 操作  INSERT 使用  insert  插入文档。 ( insert into blog.users values(“user1”,”23) ) > use blog  switched to db blog > u = { name:"user1", age:23 }   >  db.users.insert(u)
_id 和 ObjectId 作用 MongoDB 中存储的文档必须有一个“ _id” 键。这个键可以是任意类型, 默认是 ObjectId 。 ObjectId 的组合: 0 | 1 | 2 | 3  4 | 5 | 6  7 | 8  9 | 10 | 11  时间戳  机器  PID  计数器  1. 时间戳:时间不断变化 的 2. 机器:主机的唯一标识码。通常是机器主机名的散列值,这样可以确保不同主机 生成不同的 ObjectId ,不产生冲突。 3.PID: 为了确保在同一台机器上并发的多个进程产生的 ObjectId 是唯一的, 所以加上进程标识符 (PID). 4. 计数器:九个字节保证了同一秒钟不同机器不同进程产生的 ObjectId 是唯一的。 后三个字节就是一个自动增加的计数器,确保相同进程同一秒产生的 ObjectId 也是 ObjectId 也不是不一样。同一秒最多允许每个进程拥有 16 777 216 个不同的 ObjectId 。
2. Query MongoDB  支持多种复杂的查询方式,能实现大多数  T-SQL  功能,远不是  Key-Value  之类的  NoSQL DB  所能比拟的。 相关函数操作看上去非常像  .NET/C# Linq Method Syntax 。 有关查询优化和索引的细节请参考后文。 主要用到的查询函数式  find()  和  findOne() ,前者返回一个迭代器  cursor ,后者返回单个文档。 WHERE #  select * from users where name = 'user1' > db.users.find({name:"user1"}) { "_id" : ObjectId("4c4528a0b55f2224d447e4b0"), "name" : "user1", "age" : 21, "sex" : 1 } #  select * from users where name = 'user1' and age = 21 > db.users.find({name:"user1", age:21}) { "_id" : ObjectId("4c4528a0b55f2224d447e4b0"), "name" : "user1", "age" : 21, "sex" : 1 } MongoDB 操作
FIELDS #  select name, age from users where age = 21 > db.users.find({age:21}, {'name':1, 'age':1}) { &quot;_id&quot; : ObjectId(&quot;4c452c343d48c8f284b388e0&quot;), &quot;name&quot; : &quot;user1&quot;, &quot;age&quot; : 21 } #  select name, age from users > db.users.find({}, {'name':1, 'age':1}) SORT #  select * from users order by age > db.users.find().sort({age:1}) #  select * from users order by sex asce, age desc > db.users.find().sort({sex:1, age:-1}) SLICE #  select * from users skip 2 limit 3 > db.users.find().skip(2).limit(3) Conditional Operators #  select * from users where sex = 1 and age > 23 and age < 28 > db.users.find({sex:1, age:{$gt:23, $lt:28}}) 比较操作包括: $gt (>) 、 $lt (<) 、 $gte (>=) 、 $lte(<=) 、 $ne (!=) 。
(6) IN #  select * from users where age in (23, 26, 32) > db.users.find({age:{$in:[23,26,32]}}) 对应的操作符有  $nin (not in) 。 (7) COUNT # s elect count(*) from users where age > 30 > db.users.find({age:{$gt:30}}).count() OR #  select * from users where age = 25 or age = 28 # select * from users where age <= 23 or age >= 33 > db.users.find({$or:[{age:25}, {age:28}]}) > db.users.find({$or:[{age:{$lte:23}}, {age:{$gte:33}}]}) MongoDB 操作
Update 可直接用类似  T-SQL  条件表达式更新,或用  Save()  更新从数据库返回到文档对象。 #  update users set age = 100, sex = 0 where name = 'user1' > db.users.update({name:&quot;user1&quot;}, {$set:{age:100, sex:0}}) update()  有几个参数需要注意。 db.collection.update(criteria, objNew, upsert, mult) criteria:  需要被更新的条件表达式 objNew:  更新表达式 upsert:  如目标记录不存在,是否插入新文档。 multi:  是否更新多个文档。 #  update users set age = age + 10 > db.users.update({}, {$inc:{age:10}}, false, true) #  update users set age = age + 10, sex = 1 where name = 'user1' > db.users.update({name:&quot;user1&quot;}, {$inc:{age:10}, $set:{sex:1}}) Remove remove()  用于删除单个或全部文档,删除后的文档无法恢复。 > id = db.users.findOne({name:&quot;user2&quot;})._id ObjectId(&quot;4c4508818c4a1e0bf570460f&quot;) > db.users.remove(id) // 移除 name='use2' 的行 > db.users.remove()// 移除所有
MongoDB: Index // single ascending db.colors.ensureIndex({name: 1}) // non-blocking in background db.colors.ensureIndex({name: 1}, {background: true}) // unique db.colors.ensureIndex({email: 1}, {unique: true}) // single descending db.colors.ensureIndex({created_at: -1}) // compound > db.colors.ensureIndex({name: 1, created_at: -1})
MongoDB:Aggregation 1.> db.colors.count() >6 2. > db.colors.distinct('name') [ &quot;blue&quot;, &quot;green&quot;, &quot;orange&quot;, &quot;purple&quot;, &quot;red&quot;, &quot;yellow&quot; ] 3. > db.items.insert({title:'Home', template:'home'}) > db.items.insert({title:'What We Do', template:'page'}) > db.items.insert({title:'Our Writing', template:'page'}) > db.items.insert({title:'Who We Are', template:'page'}) > db.items.insert({title:'Hire Us', template:'page'}) > var key = {template: true}; > var initial = {count:0}; > var reduce = function(obj, prev) { prev.count += 1; }; > db.items.group({key:key, initial:initial, reduce:reduce}) [ {&quot;template&quot; : &quot;home&quot;, &quot;count&quot; : 1}, {&quot;template&quot; : &quot;page&quot;, &quot;count&quot; : 4} ]
Similar Data Types
> db.people.insert({ name : 'John', awesome : true, shows : ['Dexter', 'LOST', 'How I Met Your Mother'], info : { age : 28, home: 'South Bend, IN', dob : (new Date('November 25, 1981')) } }) > var me = db.people.findOne({name:'John'}) > me.name John > me.awesome true > me.shows[1] LOST > me.info.age 28 > me.info.dob.getFullYear() 1981
One to Many 一 、  Normalized // insert post > db.posts.insert({title:'Why Mongo Rocks'}); > var post = db.posts.findOne({title:'Why Mongo Rocks'}); // insert comment > db.comments.insert({ name :'John', body :'Because...', post_id : post._id }); var comment = db.comments.findOne({name:'John'}); > db.comments.find({post_id: post._id}) { &quot;_id&quot; : ObjectId(&quot;4bee1cc79e89db4e12bf78de&quot;), &quot;name&quot; : &quot;John&quot;, &quot;body&quot; : &quot;Because...&quot;, &quot;post_id&quot; : ObjectId(&quot;4bee1c519e89db4e12bf78dd&quot;) }
二、 Embedded 关系型数据库的存储方式:
MongoDB 的存储方式:
例如: { &quot;VendroId&quot;: 1, &quot;Name&quot;: &quot; 商家 1&quot;, &quot;ItemInfo&quot;: [ { &quot;ItemId&quot;: &quot;634422022659939961&quot;, &quot;ItemName&quot;: &quot; 商品 634422022659939961&quot;, &quot;ServiceCateId&quot;: 1 }, { &quot;ItemId&quot;: &quot;634422022659949961&quot;, &quot;ItemName&quot;: &quot; 商品 634422022659949961&quot;, &quot;ServiceCateId&quot;: 1 } ], &quot;_id&quot;: &quot;4de0c71bbeb52e0ecc000001&quot; } db.VendorInfo.find({“ItemInfo”:{“$elemMatch”:{&quot;ItemId&quot;, &quot;634422022659949961&quot;}}})
惭辞苍驳辞快速入门
Many to Many db.sites.insert({domain: 'orderedlist.com'}) db.sites.insert({domain: 'railstips.org'}) > db.sites.find() { &quot;_id&quot; : ObjectId(&quot;4bee280f9e89db4e12bf78e2&quot;), &quot;domain&quot;: &quot;orderedlist.com&quot; } { &quot;_id&quot; : ObjectId(&quot;4bee283c9e89db4e12bf78e3&quot;), &quot;domain&quot;: &quot;railstips.org&quot; }
> db.users.insert({ name: 'John', authorizations: [ ObjectId('4bee280f9e89db4e12bf78e2'), ObjectId('4bee283c9e89db4e12bf78e3') ] }) > db.users.insert({ name: 'Steve', authorizations: [ ObjectId('4bee280f9e89db4e12bf78e2') ] }) > var orderedlist = db.sites.findOne({domain:'orderedlist.com'}) db.users.find({authorizations:orderedlist._id}) > var railstips = db.sites.findOne({domain:'railstips.org'}) db.users.find({authorizations:railstips._id}) > var john = db.users.findOne({name:'John'}) > db.sites.find({_id:{$in: john.authorizations}})
Tools Performance Indicators Remedies
1. mongostat
2. serverStatus
3. Profiler ? > db. setProfilingLevel (2) { &quot;was&quot; : 0, &quot;slowms&quot; : 100, &quot;ok&quot; : 1 } > db.system.profile. find (). sort ({$natural: -1}) { &quot;ts&quot; :  ISODate (&quot;2011-05-24T14:20:09.711Z&quot;), &quot;info&quot; : &quot;query docs.spreadsheets reslen:257 nscanned:1805535 query: { query: {}, $explain: true } nreturned:1 1407ms&quot;, &quot;millis&quot; : 1407 }
Memcached 与 MongoDB 结合
惭辞苍驳辞快速入门
惭辞苍驳辞快速入门
惭辞苍驳辞快速入门
惭辞苍驳辞快速入门
?

More Related Content

What's hot (17)

厂辫谤测框架的简单使用小结
厂辫谤测框架的简单使用小结厂辫谤测框架的简单使用小结
厂辫谤测框架的简单使用小结
sunnylqm
?
Js tree
Js treeJs tree
Js tree
lotusfox
?
Mongodb
MongodbMongodb
Mongodb
bj
?
搁别诲颈蝉中文入门手册
搁别诲颈蝉中文入门手册搁别诲颈蝉中文入门手册
搁别诲颈蝉中文入门手册
www.tujia.com
?
Elasticsearch 實戰介紹
Elasticsearch 實戰介紹Elasticsearch 實戰介紹
Elasticsearch 實戰介紹
Kang-min Liu
?
Python vs json 玩open data
Python vs json 玩open dataPython vs json 玩open data
Python vs json 玩open data
政斌 楊
?
笔测迟丑辞苍速成指南
笔测迟丑辞苍速成指南笔测迟丑辞苍速成指南
笔测迟丑辞苍速成指南
March Liu
?
Json tutorial
Json tutorialJson tutorial
Json tutorial
cri fan
?
我对后端优化的一点想法.辫辫迟虫
我对后端优化的一点想法.辫辫迟虫我对后端优化的一点想法.辫辫迟虫
我对后端优化的一点想法.辫辫迟虫
james tong
?
Practical Data Analysis in R
Practical Data Analysis in RPractical Data Analysis in R
Practical Data Analysis in R
Chun-Ming Chang
?
Row Set初步学习V1.1
Row Set初步学习V1.1Row Set初步学习V1.1
Row Set初步学习V1.1
Zianed Hou
?
107个常用javascript语句 oss 计算技术 - ossez info of tech
107个常用javascript语句   oss 计算技术 - ossez info of tech107个常用javascript语句   oss 计算技术 - ossez info of tech
107个常用javascript语句 oss 计算技术 - ossez info of tech
YUCHENG HU
?
Lucene2 4学习笔记1
Lucene2 4学习笔记1Lucene2 4学习笔记1
Lucene2 4学习笔记1
yiditushe
?
Mongo db部署架构之优先方案
Mongo db部署架构之优先方案Mongo db部署架构之优先方案
Mongo db部署架构之优先方案
Lucien Li
?
Script with engine
Script with engineScript with engine
Script with engine
Webrebuild
?
Free Marker中文文档
Free Marker中文文档Free Marker中文文档
Free Marker中文文档
yiditushe
?
厂辫谤测框架的简单使用小结
厂辫谤测框架的简单使用小结厂辫谤测框架的简单使用小结
厂辫谤测框架的简单使用小结
sunnylqm
?
Mongodb
MongodbMongodb
Mongodb
bj
?
搁别诲颈蝉中文入门手册
搁别诲颈蝉中文入门手册搁别诲颈蝉中文入门手册
搁别诲颈蝉中文入门手册
www.tujia.com
?
Elasticsearch 實戰介紹
Elasticsearch 實戰介紹Elasticsearch 實戰介紹
Elasticsearch 實戰介紹
Kang-min Liu
?
Python vs json 玩open data
Python vs json 玩open dataPython vs json 玩open data
Python vs json 玩open data
政斌 楊
?
笔测迟丑辞苍速成指南
笔测迟丑辞苍速成指南笔测迟丑辞苍速成指南
笔测迟丑辞苍速成指南
March Liu
?
Json tutorial
Json tutorialJson tutorial
Json tutorial
cri fan
?
我对后端优化的一点想法.辫辫迟虫
我对后端优化的一点想法.辫辫迟虫我对后端优化的一点想法.辫辫迟虫
我对后端优化的一点想法.辫辫迟虫
james tong
?
Practical Data Analysis in R
Practical Data Analysis in RPractical Data Analysis in R
Practical Data Analysis in R
Chun-Ming Chang
?
Row Set初步学习V1.1
Row Set初步学习V1.1Row Set初步学习V1.1
Row Set初步学习V1.1
Zianed Hou
?
107个常用javascript语句 oss 计算技术 - ossez info of tech
107个常用javascript语句   oss 计算技术 - ossez info of tech107个常用javascript语句   oss 计算技术 - ossez info of tech
107个常用javascript语句 oss 计算技术 - ossez info of tech
YUCHENG HU
?
Lucene2 4学习笔记1
Lucene2 4学习笔记1Lucene2 4学习笔记1
Lucene2 4学习笔记1
yiditushe
?
Mongo db部署架构之优先方案
Mongo db部署架构之优先方案Mongo db部署架构之优先方案
Mongo db部署架构之优先方案
Lucien Li
?
Script with engine
Script with engineScript with engine
Script with engine
Webrebuild
?
Free Marker中文文档
Free Marker中文文档Free Marker中文文档
Free Marker中文文档
yiditushe
?

Viewers also liked (20)

03,04,05,06,07MARCO.pdf
03,04,05,06,07MARCO.pdf03,04,05,06,07MARCO.pdf
03,04,05,06,07MARCO.pdf
direitoshumanosbm
?
Na pocz?tku jest Google – reklama w wyszukiwarce. Linki sponsorowane Google A...
Na pocz?tku jest Google – reklama w wyszukiwarce. Linki sponsorowane Google A...Na pocz?tku jest Google – reklama w wyszukiwarce. Linki sponsorowane Google A...
Na pocz?tku jest Google – reklama w wyszukiwarce. Linki sponsorowane Google A...
B?a?ej Abel
?
1a parte: Storia del moderno sistema economico-finanziario
1a parte: Storia del moderno sistema economico-finanziario1a parte: Storia del moderno sistema economico-finanziario
1a parte: Storia del moderno sistema economico-finanziario
elegias
?
PsycINFO in 10steps 042611
PsycINFO in 10steps 042611PsycINFO in 10steps 042611
PsycINFO in 10steps 042611
kncarlso
?
Ip demo
Ip demoIp demo
Ip demo
nevatech
?
Mobilní za?ízení jako nov? prodejní kanál
Mobilní za?ízení jako nov? prodejní kanálMobilní za?ízení jako nov? prodejní kanál
Mobilní za?ízení jako nov? prodejní kanál
eMan s.r.o.
?
Uitnodiging kempenconferentie.pps
Uitnodiging kempenconferentie.ppsUitnodiging kempenconferentie.pps
Uitnodiging kempenconferentie.pps
DJAN01
?
Content management for the cloud cloud slam 2011_final
Content management for the cloud cloud slam 2011_finalContent management for the cloud cloud slam 2011_final
Content management for the cloud cloud slam 2011_final
ABBYY
?
Asma3Asma3
Asma3
Jael Herrera Vasquez
?
Presentasi Leon Kamilius SxC Summit
Presentasi Leon Kamilius SxC SummitPresentasi Leon Kamilius SxC Summit
Presentasi Leon Kamilius SxC Summit
samiasafa
?
Manzanar presentation
Manzanar presentationManzanar presentation
Manzanar presentation
jillmarucut
?
Yummy
YummyYummy
Yummy
tahniemonique
?
Case study Mundo de Estrellas
Case study Mundo de EstrellasCase study Mundo de Estrellas
Case study Mundo de Estrellas
Links-up
?
путешествие в страну вышивания
путешествие в страну вышиванияпутешествие в страну вышивания
путешествие в страну вышивания
Noskova
?
Case study Cyberhus
Case study CyberhusCase study Cyberhus
Case study Cyberhus
Links-up
?
Recursos informacionais - Biblioteca EEFE-USPRecursos informacionais - Biblioteca EEFE-USP
Recursos informacionais - Biblioteca EEFE-USP
Solange Santana
?
Introduction to Learning Circles & Nopros
Introduction to Learning Circles & NoprosIntroduction to Learning Circles & Nopros
Introduction to Learning Circles & Nopros
Colin Campbell
?
Komasky sleep
Komasky sleepKomasky sleep
Komasky sleep
szmulew
?
Na pocz?tku jest Google – reklama w wyszukiwarce. Linki sponsorowane Google A...
Na pocz?tku jest Google – reklama w wyszukiwarce. Linki sponsorowane Google A...Na pocz?tku jest Google – reklama w wyszukiwarce. Linki sponsorowane Google A...
Na pocz?tku jest Google – reklama w wyszukiwarce. Linki sponsorowane Google A...
B?a?ej Abel
?
1a parte: Storia del moderno sistema economico-finanziario
1a parte: Storia del moderno sistema economico-finanziario1a parte: Storia del moderno sistema economico-finanziario
1a parte: Storia del moderno sistema economico-finanziario
elegias
?
PsycINFO in 10steps 042611
PsycINFO in 10steps 042611PsycINFO in 10steps 042611
PsycINFO in 10steps 042611
kncarlso
?
Mobilní za?ízení jako nov? prodejní kanál
Mobilní za?ízení jako nov? prodejní kanálMobilní za?ízení jako nov? prodejní kanál
Mobilní za?ízení jako nov? prodejní kanál
eMan s.r.o.
?
Uitnodiging kempenconferentie.pps
Uitnodiging kempenconferentie.ppsUitnodiging kempenconferentie.pps
Uitnodiging kempenconferentie.pps
DJAN01
?
Content management for the cloud cloud slam 2011_final
Content management for the cloud cloud slam 2011_finalContent management for the cloud cloud slam 2011_final
Content management for the cloud cloud slam 2011_final
ABBYY
?
Presentasi Leon Kamilius SxC Summit
Presentasi Leon Kamilius SxC SummitPresentasi Leon Kamilius SxC Summit
Presentasi Leon Kamilius SxC Summit
samiasafa
?
Manzanar presentation
Manzanar presentationManzanar presentation
Manzanar presentation
jillmarucut
?
Case study Mundo de Estrellas
Case study Mundo de EstrellasCase study Mundo de Estrellas
Case study Mundo de Estrellas
Links-up
?
путешествие в страну вышивания
путешествие в страну вышиванияпутешествие в страну вышивания
путешествие в страну вышивания
Noskova
?
Case study Cyberhus
Case study CyberhusCase study Cyberhus
Case study Cyberhus
Links-up
?
Recursos informacionais - Biblioteca EEFE-USPRecursos informacionais - Biblioteca EEFE-USP
Recursos informacionais - Biblioteca EEFE-USP
Solange Santana
?
Introduction to Learning Circles & Nopros
Introduction to Learning Circles & NoprosIntroduction to Learning Circles & Nopros
Introduction to Learning Circles & Nopros
Colin Campbell
?
Komasky sleep
Komasky sleepKomasky sleep
Komasky sleep
szmulew
?

Similar to 惭辞苍驳辞快速入门 (20)

Mongo db技术交流
Mongo db技术交流Mongo db技术交流
Mongo db技术交流
liuts
?
深入学习Mongo db
深入学习Mongo db深入学习Mongo db
深入学习Mongo db
Lucien Li
?
Mongo db技术分享
Mongo db技术分享Mongo db技术分享
Mongo db技术分享
晓锋 陈
?
【第一季第四期】JavaScript Optimization
【第一季第四期】JavaScript Optimization【第一季第四期】JavaScript Optimization
【第一季第四期】JavaScript Optimization
tbosstraining
?
Asp.net mvc 培训
Asp.net mvc 培训Asp.net mvc 培训
Asp.net mvc 培训
lotusprince
?
MongoDB Basics and Tutorial
MongoDB Basics and TutorialMongoDB Basics and Tutorial
MongoDB Basics and Tutorial
Ho Kim
?
旺铺前端设计和实现
旺铺前端设计和实现旺铺前端设计和实现
旺铺前端设计和实现
hua qiu
?
Django Firstofhexu
Django FirstofhexuDjango Firstofhexu
Django Firstofhexu
hexuotzo
?
数据处理算法设计要点
数据处理算法设计要点数据处理算法设计要点
数据处理算法设计要点
thinkinlamp
?
狈辞诲别.箩蝉开发体验
狈辞诲别.箩蝉开发体验狈辞诲别.箩蝉开发体验
狈辞诲别.箩蝉开发体验
QLeelulu
?
Google protocol buffers简析
Google protocol buffers简析Google protocol buffers简析
Google protocol buffers简析
wavefly
?
Huangjing renren
Huangjing renrenHuangjing renren
Huangjing renren
d0nn9n
?
组件交互模式的非主流研究
组件交互模式的非主流研究组件交互模式的非主流研究
组件交互模式的非主流研究
youalab
?
MongoDB for C# developer
MongoDB for C# developerMongoDB for C# developer
MongoDB for C# developer
dianming.song
?
Javascript Training
Javascript TrainingJavascript Training
Javascript Training
beijing.josh
?
追风堂 Javascript
追风堂 Javascript追风堂 Javascript
追风堂 Javascript
minipeach
?
Mastering Mustache
Mastering MustacheMastering Mustache
Mastering Mustache
tinyhill
?
PHP Coding Standard and 50+ Programming Skills
PHP Coding Standard and 50+ Programming SkillsPHP Coding Standard and 50+ Programming Skills
PHP Coding Standard and 50+ Programming Skills
Ho Kim
?
Mongo db技术交流
Mongo db技术交流Mongo db技术交流
Mongo db技术交流
liuts
?
深入学习Mongo db
深入学习Mongo db深入学习Mongo db
深入学习Mongo db
Lucien Li
?
Mongo db技术分享
Mongo db技术分享Mongo db技术分享
Mongo db技术分享
晓锋 陈
?
【第一季第四期】JavaScript Optimization
【第一季第四期】JavaScript Optimization【第一季第四期】JavaScript Optimization
【第一季第四期】JavaScript Optimization
tbosstraining
?
MongoDB Basics and Tutorial
MongoDB Basics and TutorialMongoDB Basics and Tutorial
MongoDB Basics and Tutorial
Ho Kim
?
旺铺前端设计和实现
旺铺前端设计和实现旺铺前端设计和实现
旺铺前端设计和实现
hua qiu
?
Django Firstofhexu
Django FirstofhexuDjango Firstofhexu
Django Firstofhexu
hexuotzo
?
数据处理算法设计要点
数据处理算法设计要点数据处理算法设计要点
数据处理算法设计要点
thinkinlamp
?
狈辞诲别.箩蝉开发体验
狈辞诲别.箩蝉开发体验狈辞诲别.箩蝉开发体验
狈辞诲别.箩蝉开发体验
QLeelulu
?
Google protocol buffers简析
Google protocol buffers简析Google protocol buffers简析
Google protocol buffers简析
wavefly
?
Huangjing renren
Huangjing renrenHuangjing renren
Huangjing renren
d0nn9n
?
组件交互模式的非主流研究
组件交互模式的非主流研究组件交互模式的非主流研究
组件交互模式的非主流研究
youalab
?
MongoDB for C# developer
MongoDB for C# developerMongoDB for C# developer
MongoDB for C# developer
dianming.song
?
追风堂 Javascript
追风堂 Javascript追风堂 Javascript
追风堂 Javascript
minipeach
?
Mastering Mustache
Mastering MustacheMastering Mustache
Mastering Mustache
tinyhill
?
PHP Coding Standard and 50+ Programming Skills
PHP Coding Standard and 50+ Programming SkillsPHP Coding Standard and 50+ Programming Skills
PHP Coding Standard and 50+ Programming Skills
Ho Kim
?

More from Lucien Li (6)

狈驳颈苍虫共享内存
狈驳颈苍虫共享内存狈驳颈苍虫共享内存
狈驳颈苍虫共享内存
Lucien Li
?
Mongo db部署架构之优先方案
Mongo db部署架构之优先方案Mongo db部署架构之优先方案
Mongo db部署架构之优先方案
Lucien Li
?
颈辞蝉分享
颈辞蝉分享颈辞蝉分享
颈辞蝉分享
Lucien Li
?
颈笔丑辞苍别,颈辞蝉,翱产箩别肠迟-肠基础入门
颈笔丑辞苍别,颈辞蝉,翱产箩别肠迟-肠基础入门颈笔丑辞苍别,颈辞蝉,翱产箩别肠迟-肠基础入门
颈笔丑辞苍别,颈辞蝉,翱产箩别肠迟-肠基础入门
Lucien Li
?
颈笔丑辞苍别,颈辞蝉,翱产箩别肠迟-颁基础入门
颈笔丑辞苍别,颈辞蝉,翱产箩别肠迟-颁基础入门颈笔丑辞苍别,颈辞蝉,翱产箩别肠迟-颁基础入门
颈笔丑辞苍别,颈辞蝉,翱产箩别肠迟-颁基础入门
Lucien Li
?
Mongo db架构之优先方案
Mongo db架构之优先方案Mongo db架构之优先方案
Mongo db架构之优先方案
Lucien Li
?
狈驳颈苍虫共享内存
狈驳颈苍虫共享内存狈驳颈苍虫共享内存
狈驳颈苍虫共享内存
Lucien Li
?
Mongo db部署架构之优先方案
Mongo db部署架构之优先方案Mongo db部署架构之优先方案
Mongo db部署架构之优先方案
Lucien Li
?
颈辞蝉分享
颈辞蝉分享颈辞蝉分享
颈辞蝉分享
Lucien Li
?
颈笔丑辞苍别,颈辞蝉,翱产箩别肠迟-肠基础入门
颈笔丑辞苍别,颈辞蝉,翱产箩别肠迟-肠基础入门颈笔丑辞苍别,颈辞蝉,翱产箩别肠迟-肠基础入门
颈笔丑辞苍别,颈辞蝉,翱产箩别肠迟-肠基础入门
Lucien Li
?
颈笔丑辞苍别,颈辞蝉,翱产箩别肠迟-颁基础入门
颈笔丑辞苍别,颈辞蝉,翱产箩别肠迟-颁基础入门颈笔丑辞苍别,颈辞蝉,翱产箩别肠迟-颁基础入门
颈笔丑辞苍别,颈辞蝉,翱产箩别肠迟-颁基础入门
Lucien Li
?
Mongo db架构之优先方案
Mongo db架构之优先方案Mongo db架构之优先方案
Mongo db架构之优先方案
Lucien Li
?

惭辞苍驳辞快速入门

  • 2. Why MongoDB Is Awesome
  • 3. ?
  • 4. NoSQL 四大类 1.key-value 存储 Examples Tokyo Cabinet/Tyrant, Redis, Voldemort, Oracle BDB 典型应用场景 内容缓存,主要用于处理大量数据的高访问负载,也用于一些日志系统等等。 数据模型 Key 指向 Value 的键值对,通常用 hash table 来实现 强项 查找速度快 弱项 数据无结构化,通常只被当作字符串或者二进制数据
  • 5. NoSQL 四大类 2. 列式数据库 Examples Cassandra, HBase, Riak 典型应用场景 分布式的文件系统 数据模型 以列簇式存储,将同一列数据存在一起 强项 查找速度快,可扩展性强,更容易进行分布式扩展 弱项 功能相对局限
  • 6. 3. 文档型数据库 NoSQL 四大类 Examples CouchDB, MongoDb 典型应用场景 Web 应用(与 Key-Value 类似, Value 是结构化的,不同的是数据库能够了解 Value 的内容) 数据模型 Key-Value 对应的键值对, Value 为结构化数据 强项 数据结构要求不严格,表结构可变,不需要像关系型数据库一样需要预先定义表结构 弱项 查询性能不高,而且缺乏统一的查询语法。
  • 7. 4. 图结构数据库 NoSQL 四大类 Examples Neo4J, InfoGrid, Infinite Graph 典型应用场景 社交网络,推荐系统等。专注于构建关系图谱 数据模型 图结构 强项 利用图结构相关算法。比如最短路径寻址, N 度关系查找等 弱项 很多时候需要对整个图做计算才能得出需要的信息,而且这种结构不太好做分布式的集群方案。
  • 8. MongoDB 介绍 谁在使用 ?
  • 9. MongoDB 介绍 Mongo 是一个高性能,开源,无模式的文档型数据库,它在许多场景下可用于替代传统的关系型数据库或键 / 值存储方式。 Mongo 使用 C++ 开发。
  • 10. MongoDB 介绍 特点 高性能、易部署、易使用,存储数据非常方便。主要功能特性有: 面向集合存储,易存储对象类型的数据。 模式自由。 支持动态查询。 支持完全索引,包含内部对象。 支持查询。 支持复制和故障恢复。 使用高效的二进制数据存储,包括大型对象(如视频等)。 自动处理碎片,以支持云计算层次的扩展性 支持 Python , PHP , Ruby , Java , C , C# , Javascript , Perl 及 C++ 语言的驱动程序 , 社区中也提供了对 Erlang 及 .NET 等平台的驱动程序。 文件存储格式为 BSON (一种 JSON 的扩展) 可通过网络访问
  • 11. MongoDB 介绍 适用场合 网站数据: Mongo 非常适合实时的插入,更新与查询,并具备网站实时数据存储所需的复制及高度伸缩性。 缓存:由于性能很高, Mongo 也适合作为信息基础设施的缓存层。在系统重启之后,由 Mongo 搭建的持久化缓存层可以避免下层的数据源 过载。 大尺寸,低价值的数据:使用传统的关系型数据库存储一些数据时可能会比较昂贵,在此之前,很多时候程序员往往会选择传统的文件进行存储。 高伸缩性的场景: Mongo 非常适合由数十或数百台服务器组成的数据库。 Mongo 的路线图中已经包含对 MapReduce 引擎的内置支持。 用于对象及 JSON 数据的存储: Mongo 的 BSON 数据格式非常适合文档化格式的存储及查询。
  • 12. MongoDB 介绍 不适用场合 1. 高度事务性的系统:例如银行或会计系统。传统的关系型数据库目前还是更适用于需要大量原子性复杂事务的应用程序。 2. 传统的商业智能应用:针对特定问题的 BI 数据库会对产生高度优化的查询方式。对于此类应用,数据仓库可能是更合适的选择。
  • 13. MongoDB 操作 安装 wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-1.2.4.tgz tar -zxvf mongodb-linux-x86_64-1.2.4.tgz mkdir -p /data/db cd /data/db mkdir m_data m_log cd mongodb-linux-x86_64-1.2.4 启动: bin/mongod --dbpath=/data/db/m_data --logpath=/data/db/m_log --logappend --port=27017 &
  • 19. MongoDB 操作 INSERT 使用 insert 插入文档。 ( insert into blog.users values(“user1”,”23) ) > use blog switched to db blog > u = { name:&quot;user1&quot;, age:23 } > db.users.insert(u)
  • 20. _id 和 ObjectId 作用 MongoDB 中存储的文档必须有一个“ _id” 键。这个键可以是任意类型, 默认是 ObjectId 。 ObjectId 的组合: 0 | 1 | 2 | 3 4 | 5 | 6 7 | 8 9 | 10 | 11 时间戳 机器 PID 计数器 1. 时间戳:时间不断变化 的 2. 机器:主机的唯一标识码。通常是机器主机名的散列值,这样可以确保不同主机 生成不同的 ObjectId ,不产生冲突。 3.PID: 为了确保在同一台机器上并发的多个进程产生的 ObjectId 是唯一的, 所以加上进程标识符 (PID). 4. 计数器:九个字节保证了同一秒钟不同机器不同进程产生的 ObjectId 是唯一的。 后三个字节就是一个自动增加的计数器,确保相同进程同一秒产生的 ObjectId 也是 ObjectId 也不是不一样。同一秒最多允许每个进程拥有 16 777 216 个不同的 ObjectId 。
  • 21. 2. Query MongoDB 支持多种复杂的查询方式,能实现大多数 T-SQL 功能,远不是 Key-Value 之类的 NoSQL DB 所能比拟的。 相关函数操作看上去非常像 .NET/C# Linq Method Syntax 。 有关查询优化和索引的细节请参考后文。 主要用到的查询函数式 find() 和 findOne() ,前者返回一个迭代器 cursor ,后者返回单个文档。 WHERE # select * from users where name = 'user1' > db.users.find({name:&quot;user1&quot;}) { &quot;_id&quot; : ObjectId(&quot;4c4528a0b55f2224d447e4b0&quot;), &quot;name&quot; : &quot;user1&quot;, &quot;age&quot; : 21, &quot;sex&quot; : 1 } # select * from users where name = 'user1' and age = 21 > db.users.find({name:&quot;user1&quot;, age:21}) { &quot;_id&quot; : ObjectId(&quot;4c4528a0b55f2224d447e4b0&quot;), &quot;name&quot; : &quot;user1&quot;, &quot;age&quot; : 21, &quot;sex&quot; : 1 } MongoDB 操作
  • 22. FIELDS # select name, age from users where age = 21 > db.users.find({age:21}, {'name':1, 'age':1}) { &quot;_id&quot; : ObjectId(&quot;4c452c343d48c8f284b388e0&quot;), &quot;name&quot; : &quot;user1&quot;, &quot;age&quot; : 21 } # select name, age from users > db.users.find({}, {'name':1, 'age':1}) SORT # select * from users order by age > db.users.find().sort({age:1}) # select * from users order by sex asce, age desc > db.users.find().sort({sex:1, age:-1}) SLICE # select * from users skip 2 limit 3 > db.users.find().skip(2).limit(3) Conditional Operators # select * from users where sex = 1 and age > 23 and age < 28 > db.users.find({sex:1, age:{$gt:23, $lt:28}}) 比较操作包括: $gt (>) 、 $lt (<) 、 $gte (>=) 、 $lte(<=) 、 $ne (!=) 。
  • 23. (6) IN # select * from users where age in (23, 26, 32) > db.users.find({age:{$in:[23,26,32]}}) 对应的操作符有 $nin (not in) 。 (7) COUNT # s elect count(*) from users where age > 30 > db.users.find({age:{$gt:30}}).count() OR # select * from users where age = 25 or age = 28 # select * from users where age <= 23 or age >= 33 > db.users.find({$or:[{age:25}, {age:28}]}) > db.users.find({$or:[{age:{$lte:23}}, {age:{$gte:33}}]}) MongoDB 操作
  • 24. Update 可直接用类似 T-SQL 条件表达式更新,或用 Save() 更新从数据库返回到文档对象。 # update users set age = 100, sex = 0 where name = 'user1' > db.users.update({name:&quot;user1&quot;}, {$set:{age:100, sex:0}}) update() 有几个参数需要注意。 db.collection.update(criteria, objNew, upsert, mult) criteria: 需要被更新的条件表达式 objNew: 更新表达式 upsert: 如目标记录不存在,是否插入新文档。 multi: 是否更新多个文档。 # update users set age = age + 10 > db.users.update({}, {$inc:{age:10}}, false, true) # update users set age = age + 10, sex = 1 where name = 'user1' > db.users.update({name:&quot;user1&quot;}, {$inc:{age:10}, $set:{sex:1}}) Remove remove() 用于删除单个或全部文档,删除后的文档无法恢复。 > id = db.users.findOne({name:&quot;user2&quot;})._id ObjectId(&quot;4c4508818c4a1e0bf570460f&quot;) > db.users.remove(id) // 移除 name='use2' 的行 > db.users.remove()// 移除所有
  • 25. MongoDB: Index // single ascending db.colors.ensureIndex({name: 1}) // non-blocking in background db.colors.ensureIndex({name: 1}, {background: true}) // unique db.colors.ensureIndex({email: 1}, {unique: true}) // single descending db.colors.ensureIndex({created_at: -1}) // compound > db.colors.ensureIndex({name: 1, created_at: -1})
  • 26. MongoDB:Aggregation 1.> db.colors.count() >6 2. > db.colors.distinct('name') [ &quot;blue&quot;, &quot;green&quot;, &quot;orange&quot;, &quot;purple&quot;, &quot;red&quot;, &quot;yellow&quot; ] 3. > db.items.insert({title:'Home', template:'home'}) > db.items.insert({title:'What We Do', template:'page'}) > db.items.insert({title:'Our Writing', template:'page'}) > db.items.insert({title:'Who We Are', template:'page'}) > db.items.insert({title:'Hire Us', template:'page'}) > var key = {template: true}; > var initial = {count:0}; > var reduce = function(obj, prev) { prev.count += 1; }; > db.items.group({key:key, initial:initial, reduce:reduce}) [ {&quot;template&quot; : &quot;home&quot;, &quot;count&quot; : 1}, {&quot;template&quot; : &quot;page&quot;, &quot;count&quot; : 4} ]
  • 28. > db.people.insert({ name : 'John', awesome : true, shows : ['Dexter', 'LOST', 'How I Met Your Mother'], info : { age : 28, home: 'South Bend, IN', dob : (new Date('November 25, 1981')) } }) > var me = db.people.findOne({name:'John'}) > me.name John > me.awesome true > me.shows[1] LOST > me.info.age 28 > me.info.dob.getFullYear() 1981
  • 29. One to Many 一 、 Normalized // insert post > db.posts.insert({title:'Why Mongo Rocks'}); > var post = db.posts.findOne({title:'Why Mongo Rocks'}); // insert comment > db.comments.insert({ name :'John', body :'Because...', post_id : post._id }); var comment = db.comments.findOne({name:'John'}); > db.comments.find({post_id: post._id}) { &quot;_id&quot; : ObjectId(&quot;4bee1cc79e89db4e12bf78de&quot;), &quot;name&quot; : &quot;John&quot;, &quot;body&quot; : &quot;Because...&quot;, &quot;post_id&quot; : ObjectId(&quot;4bee1c519e89db4e12bf78dd&quot;) }
  • 32. 例如: { &quot;VendroId&quot;: 1, &quot;Name&quot;: &quot; 商家 1&quot;, &quot;ItemInfo&quot;: [ { &quot;ItemId&quot;: &quot;634422022659939961&quot;, &quot;ItemName&quot;: &quot; 商品 634422022659939961&quot;, &quot;ServiceCateId&quot;: 1 }, { &quot;ItemId&quot;: &quot;634422022659949961&quot;, &quot;ItemName&quot;: &quot; 商品 634422022659949961&quot;, &quot;ServiceCateId&quot;: 1 } ], &quot;_id&quot;: &quot;4de0c71bbeb52e0ecc000001&quot; } db.VendorInfo.find({“ItemInfo”:{“$elemMatch”:{&quot;ItemId&quot;, &quot;634422022659949961&quot;}}})
  • 34. Many to Many db.sites.insert({domain: 'orderedlist.com'}) db.sites.insert({domain: 'railstips.org'}) > db.sites.find() { &quot;_id&quot; : ObjectId(&quot;4bee280f9e89db4e12bf78e2&quot;), &quot;domain&quot;: &quot;orderedlist.com&quot; } { &quot;_id&quot; : ObjectId(&quot;4bee283c9e89db4e12bf78e3&quot;), &quot;domain&quot;: &quot;railstips.org&quot; }
  • 35. > db.users.insert({ name: 'John', authorizations: [ ObjectId('4bee280f9e89db4e12bf78e2'), ObjectId('4bee283c9e89db4e12bf78e3') ] }) > db.users.insert({ name: 'Steve', authorizations: [ ObjectId('4bee280f9e89db4e12bf78e2') ] }) > var orderedlist = db.sites.findOne({domain:'orderedlist.com'}) db.users.find({authorizations:orderedlist._id}) > var railstips = db.sites.findOne({domain:'railstips.org'}) db.users.find({authorizations:railstips._id}) > var john = db.users.findOne({name:'John'}) > db.sites.find({_id:{$in: john.authorizations}})
  • 39. 3. Profiler ? > db. setProfilingLevel (2) { &quot;was&quot; : 0, &quot;slowms&quot; : 100, &quot;ok&quot; : 1 } > db.system.profile. find (). sort ({$natural: -1}) { &quot;ts&quot; : ISODate (&quot;2011-05-24T14:20:09.711Z&quot;), &quot;info&quot; : &quot;query docs.spreadsheets reslen:257 nscanned:1805535 query: { query: {}, $explain: true } nreturned:1 1407ms&quot;, &quot;millis&quot; : 1407 }
  • 45. ?