SlideShare a Scribd company logo
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 !

More Related Content

PPT
Nosql七种武器之长生剑 mongodb的使用介绍
PPT
Mongo简介
PDF
Mongo基础
PDF
MongoDB Basic
PPT
深入学习Mongo db
PPT
Mongo db技术分享
PDF
MongoDB for C# developer
PDF
D2 如何发现前端性能问题
Nosql七种武器之长生剑 mongodb的使用介绍
Mongo简介
Mongo基础
MongoDB Basic
深入学习Mongo db
Mongo db技术分享
MongoDB for C# developer
D2 如何发现前端性能问题

What's hot (20)

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

Viewers also liked (20)

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

Similar to MongoDB Basics and Tutorial (20)

PDF
Mongo db introduction
PDF
Mongo db实战
PPT
Mongo快速入门
PPT
Mongo db技术分享
PDF
Mongodb学习手册
PDF
Nosql及其主要产品简介
PDF
Mongo基础
PDF
110412 kningsoft-mongo db-intro-usage-in-mercury
PPTX
NoSQL-MongoDB介紹
PDF
MongoDB at Qihoo 360
PPTX
Comment System of 56.com
PPTX
MongoDB SHARE
PPTX
Maven & mongo & sring
PPTX
Mongodb 20110610
PPTX
NoSQL-MongoDB
PPTX
Mongo db 簡介
PDF
LazyRecord: The Fast ORM for PHP
PPTX
1到100000000 - 分布式大型网站的架构设计
PDF
Couchdb Beijing Openparty
PPT
Mongodb
 
Mongo db introduction
Mongo db实战
Mongo快速入门
Mongo db技术分享
Mongodb学习手册
Nosql及其主要产品简介
Mongo基础
110412 kningsoft-mongo db-intro-usage-in-mercury
NoSQL-MongoDB介紹
MongoDB at Qihoo 360
Comment System of 56.com
MongoDB SHARE
Maven & mongo & sring
Mongodb 20110610
NoSQL-MongoDB
Mongo db 簡介
LazyRecord: The Fast ORM for PHP
1到100000000 - 分布式大型网站的架构设计
Couchdb Beijing Openparty
Mongodb
 

More from Ho Kim (12)

PDF
解决Lvs上行丢包的过程和收获
PDF
40 Powerful Shortcuts of Xcode 6.x
PPTX
Project Management Using Redmine
PPTX
OpenResty/Lua 70+ Advanced Programming Skills and Optimization tips
PPTX
Web Caching Architecture and Design
PPT
Lua 30+ Programming Skills and 20+ Optimization Tips
PPTX
人人-56 账号拆分项目总结
PPTX
OpenResty/Lua Practical Experience
PPTX
JavaScript 80+ Programming and Optimization Skills
PPT
Character Encoding and Database Transcoding Project
PPT
Video Upload Architecture of 56.com
PPTX
Git Essence Tutorial
解决Lvs上行丢包的过程和收获
40 Powerful Shortcuts of Xcode 6.x
Project Management Using Redmine
OpenResty/Lua 70+ Advanced Programming Skills and Optimization tips
Web Caching Architecture and Design
Lua 30+ Programming Skills and 20+ Optimization Tips
人人-56 账号拆分项目总结
OpenResty/Lua Practical Experience
JavaScript 80+ Programming and Optimization Skills
Character Encoding and Database Transcoding Project
Video Upload Architecture of 56.com
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