20
MongoDB MongoDB 入入 入入 技技技 技技技 kim kim

MongoDB Tutorial

  • Upload
    ho-kim

  • View
    1.979

  • Download
    18

Embed Size (px)

DESCRIPTION

MongoDB Tutorial

Citation preview

Page 1: MongoDB Tutorial

MongoDBMongoDB 入门入门技术部 技术部 kimkim

Page 2: MongoDB Tutorial

NoSQLNoSQL ??1 Not Only SQL1 Not Only SQL2. 2. 不使用不使用 SQLSQL 作为查询语言作为查询语言3. 3. 数据存储不是固定的表格模式数据存储不是固定的表格模式4. 4. 避免使用避免使用 SQLSQL 的的 JOINJOIN5. 5. 水平可扩展性水平可扩展性6. 6. 重视使用硬盘重视使用硬盘 , , 尽可能的利用 尽可能的利用 RAMRAM 做存做存

储储

Page 3: MongoDB Tutorial

1. Google 1. Google 的 的 BigTable BigTable 和 和 Amazon Amazon 的 的 DDynamoynamo

2. 2. 介绍链接:介绍链接:

http://baike.baidu.com/view/2677528.http://baike.baidu.com/view/2677528.htmhtm

http://http://zh.wikipedia.org/wiki/Nosqlzh.wikipedia.org/wiki/Nosql

Page 4: MongoDB Tutorial

MongoDBMongoDB ??1.1. 介于关系数据库和非关系数据库之间介于关系数据库和非关系数据库之间2.2. 它的数据结构非常松散,是类似它的数据结构非常松散,是类似 JSONJSON 的的 BSONBSON

格式,可以存储比较复杂的数据类型。格式,可以存储比较复杂的数据类型。3.3. MongoDBMongoDB 支持的查询语言非常强大,语法类似支持的查询语言非常强大,语法类似

于面向对象的查询语言,几乎可以实现类似关系于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能数据库单表查询的绝大部分功能

4.4. 支持对数据建立索引。支持对数据建立索引。5.5. 高性能、易部署、易使用,存储数据非常方便。高性能、易部署、易使用,存储数据非常方便。

Page 5: MongoDB Tutorial

1. 1. 官方网站:官方网站: http://www.mongodb.org/http://www.mongodb.org/2. 2.

http://baike.baidu.com/view/3385614.hhttp://baike.baidu.com/view/3385614.htmtm

Page 6: MongoDB Tutorial

存储类型:存储类型: BSONBSON BSON documents (objects) consist of a well ordered list of eleBSON documents (objects) consist of a well ordered list of ele

ments. Each element consists of a field name, a type, and a valments. Each element consists of a field name, a type, and a value. Field names are strings. Types include: ue. Field names are strings. Types include:

* string * string * integer * integer * double * double * date * date * byte array (binary data) * byte array (binary data) * boolean (true and false) * boolean (true and false) * null * null * BSON object * BSON object This is nominally a superset of JSON types (JSON does not havThis is nominally a superset of JSON types (JSON does not hav

e a byte array type, for example), but because of length limitate a byte array type, for example), but because of length limitations, some valid JSON values (such as very long strings) are noions, some valid JSON values (such as very long strings) are not valid BSON values.t valid BSON values.

Page 7: MongoDB Tutorial

MongoDBMongoDB 数据类型数据类型 数据类型:数据类型:

http://www.mongodb.org/display/DOChttp://www.mongodb.org/display/DOCS/Data+Types+and+ConventionsS/Data+Types+and+Conventions

对应的对应的 PHPPHP 拓展中的类型:拓展中的类型: http://http://www.php.net/manual/en/mongo.typeswww.php.net/manual/en/mongo.types.php.php

Page 8: MongoDB Tutorial

MongoDBMongoDB 安装,运行安装,运行 下载:下载: http://http://www.mongodb.orgwww.mongodb.org/downloads/downloads

WindowsWindows 下运行:下运行: C:\> mkdir \data C:\> mkdir \data C:\> mkdir \data\dbC:\> mkdir \data\db C:\> cd \my_mongo_dir\binC:\> cd \my_mongo_dir\bin C:\my_mongo_dir\bin> mongodC:\my_mongo_dir\bin> mongod

以以 ReplicaSetReplicaSet 的方式运行:的方式运行: http://www.mongodb.org/display/Dhttp://www.mongodb.org/display/DOCS/Replica+Set+TutorialOCS/Replica+Set+Tutorial

演示演示

Page 9: MongoDB Tutorial

MongoDBMongoDB 的的 PHPPHP 拓展拓展 下载下载 dlldll ::

http://www.php.net/manual/en/monghttp://www.php.net/manual/en/mongo.installation.php#mongo.installation.o.installation.php#mongo.installation.windowswindows

配置配置 php.iniphp.ini extension=php_mongo.dllextension=php_mongo.dll

Page 10: MongoDB Tutorial

AdminAdmin 及客户端及客户端 MongoDBMongoDB 自带客户端:自带客户端: http://localhost:28018/http://localhost:28018/

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

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

以以 shellshell 脚本操作脚本操作 MongoDBMongoDB :: http://http://www.mongodb.org/display/DOCS/Scripting+the+shwww.mongodb.org/display/DOCS/Scripting+the+shellell

Page 11: MongoDB Tutorial

MongoDBMongoDB 查询操作查询操作 // // 比如比如 , select * from things where x=3 and y="foo" , select * from things where x=3 and y="foo" db.things.find( { x : 3, y : "foo" } ); db.things.find( { x : 3, y : "foo" } );

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

详细资料:详细资料: http://http://www.mongodb.org/display/DOCS/Advanced+Queriwww.mongodb.org/display/DOCS/Advanced+Querieses

Page 12: MongoDB Tutorial

MongoDB Native DriverMongoDB Native Driver Mongo Mongo 类类(类似于(类似于 MySQLMySQL 基类)基类)

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

数据集核心类 数据集核心类 MongoCollectionMongoCollection

结果集 结果集 MongoCursorMongoCursor(注意可以通过(注意可以通过 iterator_to_arraiterator_to_array y 转换成数组,可以通过 转换成数组,可以通过 count count 直接获取总数)直接获取总数)

1. $cursor = $collection->find();1. $cursor = $collection->find(); $array = iterator_to_array($cursor); $array = iterator_to_array($cursor);

2. $total = MongoCursor->count(false) ; 2. $total = MongoCursor->count(false) ;

Page 13: MongoDB Tutorial

Cursor StagesCursor Stages A MongoCursor has two "life stages": pre- and pA MongoCursor has two "life stages": pre- and p

ost- query. When a cursor is created, it has not ost- query. When a cursor is created, it has not yet contacted the database, so it is in its pre-quyet contacted the database, so it is in its pre-query state. In this state, the client can further spery state. In this state, the client can further specify what they want the query to do, including ecify what they want the query to do, including adding limits, skips, sorts, and more advanced adding limits, skips, sorts, and more advanced options.options.

When the client attempts to get a result (by callWhen the client attempts to get a result (by calling MongoCursor::next(), directly or indirectly),ing MongoCursor::next(), directly or indirectly), the cursor moves into the post-query stage. At the cursor moves into the post-query stage. At this point, the query has been executed by the this point, the query has been executed by the database and cannot be modified anymore.database and cannot be modified anymore.

Page 14: MongoDB Tutorial

外键?引用,外键?引用, DBRefDBRef 机制机制 通常用于处理复杂的数据结构通常用于处理复杂的数据结构

MongoCollection::MongoCollection::createcreateDBRefDBRef

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

{{ "_id": ObjectId("4f3cddfc8634d3ec01000000"),"_id": ObjectId("4f3cddfc8634d3ec01000000"), "vid": 66200767,"vid": 66200767, ...... "created_at": 1329389052,"created_at": 1329389052, "comment_ref": {"comment_ref": { "$ref": "comment","$ref": "comment", "$id": ObjectId("4f3cdd2e8634d36803000002") "$id": ObjectId("4f3cdd2e8634d36803000002") } } }}

Page 15: MongoDB Tutorial

SQL to MongoDBSQL to MongoDB

例如:例如: SELECT * FROM users WHERE age=33 对应的是: $db->users->find(array("age" => 33));

官网对照表官网对照表

PHPPHP对照表对照表

Page 16: MongoDB Tutorial

统计与 统计与 MapReduceMapReduce 基本概念(冗长):基本概念(冗长):

http://www.mongodb.org/display/DOChttp://www.mongodb.org/display/DOCS/MapReduceS/MapReduce

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

PHPPHP拓展里面的例子拓展里面的例子

Page 17: MongoDB Tutorial

注意事项注意事项1.1. 不推荐使用主从,而是使用 不推荐使用主从,而是使用 ReplicaSetReplicaSet2.2. 当其中一个当其中一个 secondarysecondary 出错之后,貌似是会拖出错之后,貌似是会拖

慢整个查询,必须有一个快速反应的机制慢整个查询,必须有一个快速反应的机制3.3. 保持奇数个数的 保持奇数个数的 Server Server 4.4. db.addUser(“kim”, “password”) db.addUser(“kim”, “password”) 为数为数

据库添加用户并设置密码据库添加用户并设置密码5.5. PHPPHP 拓展里面的 拓展里面的 connect connect 总是返回总是返回 truetrue https://bugs.php.net/bug.php?id=60508 https://bugs.php.net/bug.php?id=60508 6.6. Update Update 和 和 InsertInsert7.7. 数据类型要严格,数字就应该是数据类型要严格,数字就应该是 intint ,而不是字,而不是字

符串符串

Page 18: MongoDB Tutorial

经验分享经验分享 挺好的博客:挺好的博客: http://blog.nosqlfan.com/http://blog.nosqlfan.com/

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

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

手册:手册: http://www.mongodb.org/display/DOCShttp://www.mongodb.org/display/DOCS/Home/Home

Page 19: MongoDB Tutorial

PHP PHP 拓展的封装拓展的封装 https://github.com/xqpmjh/Trickle/blohttps://github.com/xqpmjh/Trickle/blo

b/master/include/class.MongoAdapter.b/master/include/class.MongoAdapter.phpphp

Page 20: MongoDB Tutorial

Thank youThank you !!