MongoDB Tutorial
Upcoming SlideShare
Loading in...5
×
 

MongoDB Tutorial

on

  • 2,345 views

MongoDB Tutorial

MongoDB Tutorial

Statistics

Views

Total Views
2,345
Slideshare-icon Views on SlideShare
1,586
Embed Views
759

Actions

Likes
0
Downloads
17
Comments
0

2 Embeds 759

http://www.kimbs.cn 758
http://cache.baidu.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    MongoDB Tutorial MongoDB Tutorial Presentation Transcript

    • MongoDB 入门 技术部 kim
    • NoSQL ?1 Not Only SQL2. 不使用 SQL 作为查询语言3. 数据存储不是固定的表格模式4. 避免使用 SQL 的 JOIN5. 水平可扩展性6. 重视使用硬盘 , 尽可能的利用 RAM 做存 储
    • 1. Google 的 BigTable 和 Amazon 的 Dynamo2. 介绍链接: 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. 不推荐使用主从,而是使用 ReplicaSet2. 当其中一个 secondary 出错之后,貌似是会拖慢 整个查询,必须有一个快速反应的机制3. 保持奇数个数的 Server4. db.addUser(“kim”, “password”) 为数据库添加用 户并设置密码5. PHP 拓展里面的 connect 总是返回 true https://bugs.php.net/bug.php?id=605087. Update 和 Insert8. 数据类型要严格,数字就应该是 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 !