SlideShare una empresa de Scribd logo
1 de 20
MongoDB 入门
   技术部 kim
NoSQL ?
1 Not Only SQL
2. 不使用 SQL 作为查询语言
3. 数据存储不是固定的表格模式
4. 避免使用 SQL 的 JOIN
5. 水平可扩展性
6. 重视使用硬盘 , 尽可能的利用 RAM 做存
  储
1. Google 的 BigTable 和 Amazon 的
   Dynamo
2. 介绍链接:
   http://baike.baidu.com/view/2677528.htm
   http://zh.wikipedia.org/wiki/Nosql
MongoDB ?
1.   介于关系数据库和非关系数据库之间
2.   它的数据结构非常松散,是类似 JSON 的 BSON
     格式,可以存储比较复杂的数据类型。
3.   MongoDB 支持的查询语言非常强大,语法类似
     于面向对象的查询语言,几乎可以实现类似关系
     数据库单表查询的绝大部分功能
4.   支持对数据建立索引。
5.   高性能、易部署、易使用,存储数据非常方便。
1. 官方网站: http://www.mongodb.org/
2. http://baike.baidu.com/view/3385614.htm
存储类型: BSON
   BSON documents (objects) consist of a well ordered list of elements. Each
    element consists of a field name, a type, and a value. Field names are strings.
    Types include:
      * string
      * integer
      * double
      * date
      * byte array (binary data)
      * boolean (true and false)
      * null
      * BSON object
   This is nominally a superset of JSON types (JSON does not have a byte array
    type, for example), but because of length limitations, some valid JSON values
    (such as very long strings) are not valid BSON values.
MongoDB 数据类型
   数据类型:
    http://www.mongodb.org/display/DOCS/Data+T

   对应的 PHP 拓展中的类型: http://
    www.php.net/manual/en/mongo.types.php
MongoDB 安装,运行
   下载: http://www.mongodb.org/downloads

   Windows下运行:
    C:> mkdir data
    C:> mkdir datadb
    C:> cd my_mongo_dirbin
    C:my_mongo_dirbin> mongod

   以 ReplicaSet 的方式运行:
    http://www.mongodb.org/display/DOCS/Replica+Set+Tutorial

   演示
MongoDB 的 PHP 拓展
   下载 dll :
    http://www.php.net/manual/en/mongo.installation

   配置 php.ini
    extension=php_mongo.dll
Admin 及客户端
   MongoDB 自带客户端:
    http://localhost:28018/

   各种客户端: http://
    www.mongodb.org/display/DOCS/Admin+UIs

   官网的测试界面: http://www.mongodb.org/#shell

   以 shell 脚本操作 MongoDB : http://
    www.mongodb.org/display/DOCS/Scripting+the+shell
MongoDB 查询操作
   // 比如 , select * from things where x=3 and y="foo"
    db.things.find( { x : 3, y : "foo" } );

   插入一条记录:
    db.things.insert({colors : ["blue", "black"]})

   详细资料: http://
    www.mongodb.org/display/DOCS/Advanced+Queries
MongoDB Native Driver
   Mongo 类(类似于 MySQL 基类)

   MongoDB 类(一般当你需要直接在数据库上进行操作的
    时候用到)

   数据集核心类 MongoCollection

   结果集 MongoCursor(注意可以通过 iterator_to_array 转
    换成数组,可以通过 count 直接获取总数)
    1. $cursor = $collection->find();
       $array = iterator_to_array($cursor);
    2. $total = MongoCursor->count(false) ;
Cursor Stages
   A MongoCursor has two "life stages": pre- and post-
    query. When a cursor is created, it has not yet contacted
    the database, so it is in its pre-query state. In this state,
    the client can further specify what they want the query
    to do, including adding limits, skips, sorts, and more
    advanced options.
   When the client attempts to get a result (by calling
    MongoCursor::next(), directly or indirectly), the cursor
    moves into the post-query stage. At this point, the
    query has been executed by the database and cannot be
    modified anymore.
外键?引用, DBRef 机制
   通常用于处理复杂的数据结构

   MongoCollection::createDBRef

   例如, comment 数据集的其中一篇文档:

{
   "_id": ObjectId("4f3cddfc8634d3ec01000000"),
   "vid": 66200767,
   ...
   "created_at": 1329389052,
   "comment_ref": {
     "$ref": "comment",
     "$id": ObjectId("4f3cdd2e8634d36803000002")
  }
}
SQL to MongoDB
   例如:
    SELECT * FROM users WHERE age=33
    对应的是:
    $db->users->find(array("age" => 33));

   官网对照表

   PHP对照表
统计与 MapReduce
   基本概念(冗长):
    http://www.mongodb.org/display/DOCS/MapRed

   一篇挺好的文章:
    http://blog.nosqlfan.com/html/469.html

   PHP拓展里面的例子
注意事项
1.   不推荐使用主从,而是使用 ReplicaSet
2.   当其中一个 secondary 出错之后,貌似是会拖慢
     整个查询,必须有一个快速反应的机制
3.   保持奇数个数的 Server
4.   db.addUser(“kim”, “password”) 为数据库添加用
     户并设置密码
5.   PHP 拓展里面的 connect 总是返回 true
     https://bugs.php.net/bug.php?id=60508
7.   Update 和 Insert
8.   数据类型要严格,数字就应该是 int ,而不是字
     符串
经验分享
   挺好的博客: http://blog.nosqlfan.com/

   一些设计模式: http://cookbook.mongodb.org/

   豆瓣小组:
    http://www.douban.com/group/mongodb/

   手册:
    http://www.mongodb.org/display/DOCS/Home
PHP 拓展的封装
   https://github.com/xqpmjh/Trickle/blob/mast
    er/include/class.MongoAdapter.php
Thank you !

Más contenido relacionado

La actualidad más candente

常用数据库的链接方法
常用数据库的链接方法常用数据库的链接方法
常用数据库的链接方法wensheng wei
 
Mongo db技术交流
Mongo db技术交流Mongo db技术交流
Mongo db技术交流liuts
 
MongoDB入门与实践
MongoDB入门与实践MongoDB入门与实践
MongoDB入门与实践mysqlops
 
MySQL源码分析.03.InnoDB 物理文件格式与数据恢复
MySQL源码分析.03.InnoDB 物理文件格式与数据恢复MySQL源码分析.03.InnoDB 物理文件格式与数据恢复
MySQL源码分析.03.InnoDB 物理文件格式与数据恢复Lixun Peng
 
Style基础优化之独角兽篇
Style基础优化之独角兽篇Style基础优化之独角兽篇
Style基础优化之独角兽篇fangdeng
 
MySQL源码分析.01.代码结构与基本流程
MySQL源码分析.01.代码结构与基本流程MySQL源码分析.01.代码结构与基本流程
MySQL源码分析.01.代码结构与基本流程Lixun Peng
 
Oracle Data Buffer Cache
Oracle Data Buffer CacheOracle Data Buffer Cache
Oracle Data Buffer CacheSky Jian
 
DataNode communicate with NameNode
DataNode communicate with NameNode DataNode communicate with NameNode
DataNode communicate with NameNode Jiang Yu
 
Mongo db &lamp;redis,nosql
Mongo db &lamp;redis,nosqlMongo db &lamp;redis,nosql
Mongo db &lamp;redis,nosqltaocheng1989
 
Mongo db部署架构之优先方案
Mongo db部署架构之优先方案Mongo db部署架构之优先方案
Mongo db部署架构之优先方案Lucien Li
 
Windows 10 install mysql 8.0.16
Windows 10 install mysql 8.0.16Windows 10 install mysql 8.0.16
Windows 10 install mysql 8.0.16songwenxuan2020
 
Mysql遇到的一些问题
Mysql遇到的一些问题Mysql遇到的一些问题
Mysql遇到的一些问题wang tongchao
 
内部MySQL培训.3.基本原理
内部MySQL培训.3.基本原理内部MySQL培训.3.基本原理
内部MySQL培训.3.基本原理Lixun Peng
 
Php及drupal性能优化系列(二)
Php及drupal性能优化系列(二)Php及drupal性能优化系列(二)
Php及drupal性能优化系列(二)Robbin Zhao
 
【第一季第四期】JavaScript Optimization
【第一季第四期】JavaScript Optimization【第一季第四期】JavaScript Optimization
【第一季第四期】JavaScript Optimizationtbosstraining
 
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 SkillsHo Kim
 
Spry框架的简单使用小结
Spry框架的简单使用小结Spry框架的简单使用小结
Spry框架的简单使用小结sunnylqm
 

La actualidad más candente (20)

常用数据库的链接方法
常用数据库的链接方法常用数据库的链接方法
常用数据库的链接方法
 
Mongo db技术交流
Mongo db技术交流Mongo db技术交流
Mongo db技术交流
 
MongoDB入门与实践
MongoDB入门与实践MongoDB入门与实践
MongoDB入门与实践
 
MySQL源码分析.03.InnoDB 物理文件格式与数据恢复
MySQL源码分析.03.InnoDB 物理文件格式与数据恢复MySQL源码分析.03.InnoDB 物理文件格式与数据恢复
MySQL源码分析.03.InnoDB 物理文件格式与数据恢复
 
Style基础优化之独角兽篇
Style基础优化之独角兽篇Style基础优化之独角兽篇
Style基础优化之独角兽篇
 
摘星
摘星摘星
摘星
 
MySQL源码分析.01.代码结构与基本流程
MySQL源码分析.01.代码结构与基本流程MySQL源码分析.01.代码结构与基本流程
MySQL源码分析.01.代码结构与基本流程
 
Oracle Data Buffer Cache
Oracle Data Buffer CacheOracle Data Buffer Cache
Oracle Data Buffer Cache
 
Json知识分享
Json知识分享Json知识分享
Json知识分享
 
DataNode communicate with NameNode
DataNode communicate with NameNode DataNode communicate with NameNode
DataNode communicate with NameNode
 
伪静态
伪静态伪静态
伪静态
 
Mongo db &lamp;redis,nosql
Mongo db &lamp;redis,nosqlMongo db &lamp;redis,nosql
Mongo db &lamp;redis,nosql
 
Mongo db部署架构之优先方案
Mongo db部署架构之优先方案Mongo db部署架构之优先方案
Mongo db部署架构之优先方案
 
Windows 10 install mysql 8.0.16
Windows 10 install mysql 8.0.16Windows 10 install mysql 8.0.16
Windows 10 install mysql 8.0.16
 
Mysql遇到的一些问题
Mysql遇到的一些问题Mysql遇到的一些问题
Mysql遇到的一些问题
 
内部MySQL培训.3.基本原理
内部MySQL培训.3.基本原理内部MySQL培训.3.基本原理
内部MySQL培训.3.基本原理
 
Php及drupal性能优化系列(二)
Php及drupal性能优化系列(二)Php及drupal性能优化系列(二)
Php及drupal性能优化系列(二)
 
【第一季第四期】JavaScript Optimization
【第一季第四期】JavaScript Optimization【第一季第四期】JavaScript Optimization
【第一季第四期】JavaScript Optimization
 
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
 
Spry框架的简单使用小结
Spry框架的简单使用小结Spry框架的简单使用小结
Spry框架的简单使用小结
 

Destacado

How to Network Effectively Using LinkedIn and BYUI Connect
How to Network Effectively Using LinkedIn and BYUI ConnectHow to Network Effectively Using LinkedIn and BYUI Connect
How to Network Effectively Using LinkedIn and BYUI ConnectSteve Davis
 
Photo Album
Photo AlbumPhoto Album
Photo Albumadyelw
 
Огниво
ОгнивоОгниво
ОгнивоSvetlana
 
Super poderes em tempos de crise
Super poderes em tempos de criseSuper poderes em tempos de crise
Super poderes em tempos de criseRui Ventura
 
Fleet Logistics Intro Presentation Eng Linkedin
Fleet Logistics Intro Presentation Eng LinkedinFleet Logistics Intro Presentation Eng Linkedin
Fleet Logistics Intro Presentation Eng Linkedinmanos99
 
PHP Optimization for Millions Visits Level
PHP Optimization for Millions Visits LevelPHP Optimization for Millions Visits Level
PHP Optimization for Millions Visits LevelHo Kim
 
05 syllabus presentation
05 syllabus presentation05 syllabus presentation
05 syllabus presentationskearney
 
The role of research libraries in a European e-science environment
The role of research libraries in a European e-science environmentThe role of research libraries in a European e-science environment
The role of research libraries in a European e-science environmentWouter Schallier
 
The role of research libraries in a European e-science environment
The role of research libraries in a European e-science environmentThe role of research libraries in a European e-science environment
The role of research libraries in a European e-science environmentWouter Schallier
 
Tips and advice june 2010
Tips and advice june 2010Tips and advice june 2010
Tips and advice june 2010deniseturner
 
1. student resume writing
1. student resume writing1. student resume writing
1. student resume writingSalib Lakchiri
 
Kv3 2009
Kv3 2009Kv3 2009
Kv3 2009SKF
 
Guirakhoo et al ChimVx-Den2
Guirakhoo et al ChimVx-Den2Guirakhoo et al ChimVx-Den2
Guirakhoo et al ChimVx-Den2Thomas Ermak
 
Communicating The Message Telstra & The Environmentpdf
Communicating The Message  Telstra & The EnvironmentpdfCommunicating The Message  Telstra & The Environmentpdf
Communicating The Message Telstra & The EnvironmentpdfTurlough Guerin
 
Comms Day Presentation Green Telecom Stream
Comms Day Presentation Green Telecom StreamComms Day Presentation Green Telecom Stream
Comms Day Presentation Green Telecom StreamTurlough Guerin
 
Makerstreet Propositie Ms
Makerstreet Propositie MsMakerstreet Propositie Ms
Makerstreet Propositie MsSuper Tangible
 

Destacado (20)

How to Network Effectively Using LinkedIn and BYUI Connect
How to Network Effectively Using LinkedIn and BYUI ConnectHow to Network Effectively Using LinkedIn and BYUI Connect
How to Network Effectively Using LinkedIn and BYUI Connect
 
Photo Album
Photo AlbumPhoto Album
Photo Album
 
Огниво
ОгнивоОгниво
Огниво
 
Gunosy2015-06-03
Gunosy2015-06-03Gunosy2015-06-03
Gunosy2015-06-03
 
Super poderes em tempos de crise
Super poderes em tempos de criseSuper poderes em tempos de crise
Super poderes em tempos de crise
 
Fleet Logistics Intro Presentation Eng Linkedin
Fleet Logistics Intro Presentation Eng LinkedinFleet Logistics Intro Presentation Eng Linkedin
Fleet Logistics Intro Presentation Eng Linkedin
 
PHP Optimization for Millions Visits Level
PHP Optimization for Millions Visits LevelPHP Optimization for Millions Visits Level
PHP Optimization for Millions Visits Level
 
Funverks 101
Funverks 101Funverks 101
Funverks 101
 
05 syllabus presentation
05 syllabus presentation05 syllabus presentation
05 syllabus presentation
 
The role of research libraries in a European e-science environment
The role of research libraries in a European e-science environmentThe role of research libraries in a European e-science environment
The role of research libraries in a European e-science environment
 
Welcome To Math III
Welcome To Math IIIWelcome To Math III
Welcome To Math III
 
The role of research libraries in a European e-science environment
The role of research libraries in a European e-science environmentThe role of research libraries in a European e-science environment
The role of research libraries in a European e-science environment
 
Tips and advice june 2010
Tips and advice june 2010Tips and advice june 2010
Tips and advice june 2010
 
1. student resume writing
1. student resume writing1. student resume writing
1. student resume writing
 
Kv3 2009
Kv3 2009Kv3 2009
Kv3 2009
 
Guirakhoo et al ChimVx-Den2
Guirakhoo et al ChimVx-Den2Guirakhoo et al ChimVx-Den2
Guirakhoo et al ChimVx-Den2
 
Communicating The Message Telstra & The Environmentpdf
Communicating The Message  Telstra & The EnvironmentpdfCommunicating The Message  Telstra & The Environmentpdf
Communicating The Message Telstra & The Environmentpdf
 
Comms Day Presentation Green Telecom Stream
Comms Day Presentation Green Telecom StreamComms Day Presentation Green Telecom Stream
Comms Day Presentation Green Telecom Stream
 
Computer Hardware
Computer HardwareComputer Hardware
Computer Hardware
 
Makerstreet Propositie Ms
Makerstreet Propositie MsMakerstreet Propositie Ms
Makerstreet Propositie Ms
 

Similar a MongoDB Basics and Tutorial

Mongo db 簡介
Mongo db 簡介Mongo db 簡介
Mongo db 簡介昱劭 劉
 
一个 Mongodb command 的前世今生
一个 Mongodb command 的前世今生一个 Mongodb command 的前世今生
一个 Mongodb command 的前世今生dennis zhuang
 
Mongo db技术分享
Mongo db技术分享Mongo db技术分享
Mongo db技术分享晓锋 陈
 
110412 kningsoft-mongo db-intro-usage-in-mercury
110412 kningsoft-mongo db-intro-usage-in-mercury110412 kningsoft-mongo db-intro-usage-in-mercury
110412 kningsoft-mongo db-intro-usage-in-mercuryZoom Quiet
 
Big Data, NoSQL, and MongoDB
Big Data, NoSQL, and MongoDBBig Data, NoSQL, and MongoDB
Big Data, NoSQL, and MongoDBMonster Supreme
 
Django敏捷开发 刘天斯
Django敏捷开发 刘天斯Django敏捷开发 刘天斯
Django敏捷开发 刘天斯liuts
 
Google protocol buffers简析
Google protocol buffers简析Google protocol buffers简析
Google protocol buffers简析wavefly
 
PHP面向对象实现数据库连接抽象
PHP面向对象实现数据库连接抽象PHP面向对象实现数据库连接抽象
PHP面向对象实现数据库连接抽象dbconf
 
淘宝网前端开发面试题
淘宝网前端开发面试题 淘宝网前端开发面试题
淘宝网前端开发面试题 Lumend
 
恶意网页分析实战
恶意网页分析实战恶意网页分析实战
恶意网页分析实战Huang Toby
 
Ria的强力后盾:rest+海量存储
Ria的强力后盾:rest+海量存储 Ria的强力后盾:rest+海量存储
Ria的强力后盾:rest+海量存储 zhen chen
 
D2-ETao-show
D2-ETao-showD2-ETao-show
D2-ETao-showleneli
 
面向开发的前端性能优化
面向开发的前端性能优化面向开发的前端性能优化
面向开发的前端性能优化li qiang
 
NodeJS基礎教學&簡介
NodeJS基礎教學&簡介NodeJS基礎教學&簡介
NodeJS基礎教學&簡介GO LL
 
Javascript代码注释及文档生成
Javascript代码注释及文档生成Javascript代码注释及文档生成
Javascript代码注释及文档生成fangdeng
 
NoSQL-MongoDB介紹
NoSQL-MongoDB介紹NoSQL-MongoDB介紹
NoSQL-MongoDB介紹國昭 張
 
配置Oracle 10g 双向流复制
配置Oracle 10g 双向流复制配置Oracle 10g 双向流复制
配置Oracle 10g 双向流复制maclean liu
 

Similar a MongoDB Basics and Tutorial (20)

Mongo db 簡介
Mongo db 簡介Mongo db 簡介
Mongo db 簡介
 
Mongodb学习手册
Mongodb学习手册Mongodb学习手册
Mongodb学习手册
 
一个 Mongodb command 的前世今生
一个 Mongodb command 的前世今生一个 Mongodb command 的前世今生
一个 Mongodb command 的前世今生
 
Mongo db技术分享
Mongo db技术分享Mongo db技术分享
Mongo db技术分享
 
110412 kningsoft-mongo db-intro-usage-in-mercury
110412 kningsoft-mongo db-intro-usage-in-mercury110412 kningsoft-mongo db-intro-usage-in-mercury
110412 kningsoft-mongo db-intro-usage-in-mercury
 
Big Data, NoSQL, and MongoDB
Big Data, NoSQL, and MongoDBBig Data, NoSQL, and MongoDB
Big Data, NoSQL, and MongoDB
 
Django敏捷开发 刘天斯
Django敏捷开发 刘天斯Django敏捷开发 刘天斯
Django敏捷开发 刘天斯
 
Google protocol buffers简析
Google protocol buffers简析Google protocol buffers简析
Google protocol buffers简析
 
PHP面向对象实现数据库连接抽象
PHP面向对象实现数据库连接抽象PHP面向对象实现数据库连接抽象
PHP面向对象实现数据库连接抽象
 
淘宝网前端开发面试题
淘宝网前端开发面试题 淘宝网前端开发面试题
淘宝网前端开发面试题
 
恶意网页分析实战
恶意网页分析实战恶意网页分析实战
恶意网页分析实战
 
Mongo db 特性
Mongo db 特性Mongo db 特性
Mongo db 特性
 
Ria的强力后盾:rest+海量存储
Ria的强力后盾:rest+海量存储 Ria的强力后盾:rest+海量存储
Ria的强力后盾:rest+海量存储
 
D2-ETao-show
D2-ETao-showD2-ETao-show
D2-ETao-show
 
面向开发的前端性能优化
面向开发的前端性能优化面向开发的前端性能优化
面向开发的前端性能优化
 
NodeJS基礎教學&簡介
NodeJS基礎教學&簡介NodeJS基礎教學&簡介
NodeJS基礎教學&簡介
 
Javascript代码注释及文档生成
Javascript代码注释及文档生成Javascript代码注释及文档生成
Javascript代码注释及文档生成
 
NoSQL-MongoDB介紹
NoSQL-MongoDB介紹NoSQL-MongoDB介紹
NoSQL-MongoDB介紹
 
配置Oracle 10g 双向流复制
配置Oracle 10g 双向流复制配置Oracle 10g 双向流复制
配置Oracle 10g 双向流复制
 
Node分享 展烨
Node分享 展烨Node分享 展烨
Node分享 展烨
 

Más de Ho Kim

解决Lvs上行丢包的过程和收获
解决Lvs上行丢包的过程和收获解决Lvs上行丢包的过程和收获
解决Lvs上行丢包的过程和收获Ho Kim
 
40 Powerful Shortcuts of Xcode 6.x
40 Powerful Shortcuts of Xcode 6.x40 Powerful Shortcuts of Xcode 6.x
40 Powerful Shortcuts of Xcode 6.xHo Kim
 
Project Management Using Redmine
Project Management Using RedmineProject Management Using Redmine
Project Management Using RedmineHo Kim
 
OpenResty/Lua 70+ Advanced Programming Skills and Optimization tips
OpenResty/Lua 70+ Advanced Programming Skills and Optimization tipsOpenResty/Lua 70+ Advanced Programming Skills and Optimization tips
OpenResty/Lua 70+ Advanced Programming Skills and Optimization tipsHo Kim
 
Web Caching Architecture and Design
Web Caching Architecture and DesignWeb Caching Architecture and Design
Web Caching Architecture and DesignHo Kim
 
Lua 30+ Programming Skills and 20+ Optimization Tips
Lua 30+ Programming Skills and 20+ Optimization TipsLua 30+ Programming Skills and 20+ Optimization Tips
Lua 30+ Programming Skills and 20+ Optimization TipsHo Kim
 
人人-56 账号拆分项目总结
人人-56 账号拆分项目总结人人-56 账号拆分项目总结
人人-56 账号拆分项目总结Ho Kim
 
OpenResty/Lua Practical Experience
OpenResty/Lua Practical ExperienceOpenResty/Lua Practical Experience
OpenResty/Lua Practical ExperienceHo Kim
 
JavaScript 80+ Programming and Optimization Skills
JavaScript 80+ Programming and Optimization SkillsJavaScript 80+ Programming and Optimization Skills
JavaScript 80+ Programming and Optimization SkillsHo Kim
 
Character Encoding and Database Transcoding Project
Character Encoding and Database Transcoding ProjectCharacter Encoding and Database Transcoding Project
Character Encoding and Database Transcoding ProjectHo Kim
 
Video Upload Architecture of 56.com
Video Upload Architecture of 56.comVideo Upload Architecture of 56.com
Video Upload Architecture of 56.comHo Kim
 
Comment System of 56.com
Comment System of 56.comComment System of 56.com
Comment System of 56.comHo Kim
 
Git Essence Tutorial
Git Essence TutorialGit Essence Tutorial
Git Essence TutorialHo Kim
 

Más de Ho Kim (13)

解决Lvs上行丢包的过程和收获
解决Lvs上行丢包的过程和收获解决Lvs上行丢包的过程和收获
解决Lvs上行丢包的过程和收获
 
40 Powerful Shortcuts of Xcode 6.x
40 Powerful Shortcuts of Xcode 6.x40 Powerful Shortcuts of Xcode 6.x
40 Powerful Shortcuts of Xcode 6.x
 
Project Management Using Redmine
Project Management Using RedmineProject Management Using Redmine
Project Management Using Redmine
 
OpenResty/Lua 70+ Advanced Programming Skills and Optimization tips
OpenResty/Lua 70+ Advanced Programming Skills and Optimization tipsOpenResty/Lua 70+ Advanced Programming Skills and Optimization tips
OpenResty/Lua 70+ Advanced Programming Skills and Optimization tips
 
Web Caching Architecture and Design
Web Caching Architecture and DesignWeb Caching Architecture and Design
Web Caching Architecture and Design
 
Lua 30+ Programming Skills and 20+ Optimization Tips
Lua 30+ Programming Skills and 20+ Optimization TipsLua 30+ Programming Skills and 20+ Optimization Tips
Lua 30+ Programming Skills and 20+ Optimization Tips
 
人人-56 账号拆分项目总结
人人-56 账号拆分项目总结人人-56 账号拆分项目总结
人人-56 账号拆分项目总结
 
OpenResty/Lua Practical Experience
OpenResty/Lua Practical ExperienceOpenResty/Lua Practical Experience
OpenResty/Lua Practical Experience
 
JavaScript 80+ Programming and Optimization Skills
JavaScript 80+ Programming and Optimization SkillsJavaScript 80+ Programming and Optimization Skills
JavaScript 80+ Programming and Optimization Skills
 
Character Encoding and Database Transcoding Project
Character Encoding and Database Transcoding ProjectCharacter Encoding and Database Transcoding Project
Character Encoding and Database Transcoding Project
 
Video Upload Architecture of 56.com
Video Upload Architecture of 56.comVideo Upload Architecture of 56.com
Video Upload Architecture of 56.com
 
Comment System of 56.com
Comment System of 56.comComment System of 56.com
Comment System of 56.com
 
Git Essence Tutorial
Git Essence TutorialGit Essence Tutorial
Git Essence Tutorial
 

MongoDB Basics and Tutorial

  • 1. MongoDB 入门 技术部 kim
  • 2. NoSQL ? 1 Not Only SQL 2. 不使用 SQL 作为查询语言 3. 数据存储不是固定的表格模式 4. 避免使用 SQL 的 JOIN 5. 水平可扩展性 6. 重视使用硬盘 , 尽可能的利用 RAM 做存 储
  • 3. 1. Google 的 BigTable 和 Amazon 的 Dynamo 2. 介绍链接: http://baike.baidu.com/view/2677528.htm http://zh.wikipedia.org/wiki/Nosql
  • 4. MongoDB ? 1. 介于关系数据库和非关系数据库之间 2. 它的数据结构非常松散,是类似 JSON 的 BSON 格式,可以存储比较复杂的数据类型。 3. MongoDB 支持的查询语言非常强大,语法类似 于面向对象的查询语言,几乎可以实现类似关系 数据库单表查询的绝大部分功能 4. 支持对数据建立索引。 5. 高性能、易部署、易使用,存储数据非常方便。
  • 5. 1. 官方网站: http://www.mongodb.org/ 2. http://baike.baidu.com/view/3385614.htm
  • 6. 存储类型: BSON  BSON documents (objects) consist of a well ordered list of elements. Each element consists of a field name, a type, and a value. Field names are strings. Types include:  * string  * integer  * double  * date  * byte array (binary data)  * boolean (true and false)  * null  * BSON object  This is nominally a superset of JSON types (JSON does not have a byte array type, for example), but because of length limitations, some valid JSON values (such as very long strings) are not valid BSON values.
  • 7. MongoDB 数据类型  数据类型: http://www.mongodb.org/display/DOCS/Data+T  对应的 PHP 拓展中的类型: http:// www.php.net/manual/en/mongo.types.php
  • 8. MongoDB 安装,运行  下载: http://www.mongodb.org/downloads  Windows下运行: C:> mkdir data C:> mkdir datadb C:> cd my_mongo_dirbin C:my_mongo_dirbin> mongod  以 ReplicaSet 的方式运行: http://www.mongodb.org/display/DOCS/Replica+Set+Tutorial  演示
  • 9. MongoDB 的 PHP 拓展  下载 dll : http://www.php.net/manual/en/mongo.installation  配置 php.ini extension=php_mongo.dll
  • 10. Admin 及客户端  MongoDB 自带客户端: http://localhost:28018/  各种客户端: http:// www.mongodb.org/display/DOCS/Admin+UIs  官网的测试界面: http://www.mongodb.org/#shell  以 shell 脚本操作 MongoDB : http:// www.mongodb.org/display/DOCS/Scripting+the+shell
  • 11. MongoDB 查询操作  // 比如 , select * from things where x=3 and y="foo" db.things.find( { x : 3, y : "foo" } );  插入一条记录: db.things.insert({colors : ["blue", "black"]})  详细资料: http:// www.mongodb.org/display/DOCS/Advanced+Queries
  • 12. MongoDB Native Driver  Mongo 类(类似于 MySQL 基类)  MongoDB 类(一般当你需要直接在数据库上进行操作的 时候用到)  数据集核心类 MongoCollection  结果集 MongoCursor(注意可以通过 iterator_to_array 转 换成数组,可以通过 count 直接获取总数) 1. $cursor = $collection->find(); $array = iterator_to_array($cursor); 2. $total = MongoCursor->count(false) ;
  • 13. Cursor Stages  A MongoCursor has two "life stages": pre- and post- query. When a cursor is created, it has not yet contacted the database, so it is in its pre-query state. In this state, the client can further specify what they want the query to do, including adding limits, skips, sorts, and more advanced options.  When the client attempts to get a result (by calling MongoCursor::next(), directly or indirectly), the cursor moves into the post-query stage. At this point, the query has been executed by the database and cannot be modified anymore.
  • 14. 外键?引用, DBRef 机制  通常用于处理复杂的数据结构  MongoCollection::createDBRef  例如, comment 数据集的其中一篇文档: { "_id": ObjectId("4f3cddfc8634d3ec01000000"), "vid": 66200767, ... "created_at": 1329389052, "comment_ref": { "$ref": "comment", "$id": ObjectId("4f3cdd2e8634d36803000002") } }
  • 15. SQL to MongoDB  例如: SELECT * FROM users WHERE age=33 对应的是: $db->users->find(array("age" => 33));  官网对照表  PHP对照表
  • 16. 统计与 MapReduce  基本概念(冗长): http://www.mongodb.org/display/DOCS/MapRed  一篇挺好的文章: http://blog.nosqlfan.com/html/469.html  PHP拓展里面的例子
  • 17. 注意事项 1. 不推荐使用主从,而是使用 ReplicaSet 2. 当其中一个 secondary 出错之后,貌似是会拖慢 整个查询,必须有一个快速反应的机制 3. 保持奇数个数的 Server 4. db.addUser(“kim”, “password”) 为数据库添加用 户并设置密码 5. PHP 拓展里面的 connect 总是返回 true https://bugs.php.net/bug.php?id=60508 7. Update 和 Insert 8. 数据类型要严格,数字就应该是 int ,而不是字 符串
  • 18. 经验分享  挺好的博客: http://blog.nosqlfan.com/  一些设计模式: http://cookbook.mongodb.org/  豆瓣小组: http://www.douban.com/group/mongodb/  手册: http://www.mongodb.org/display/DOCS/Home
  • 19. PHP 拓展的封装  https://github.com/xqpmjh/Trickle/blob/mast er/include/class.MongoAdapter.php