Your SlideShare is downloading. ×
MongoDB Basics and Tutorial
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

MongoDB Basics and Tutorial

2,027

Published on

1. NoSQL vs SQL …

1. NoSQL vs SQL
2. MongoDB introduction
3. "BSON", data storage type
4. Service/admin/client installations
5. Some basic operations

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,027
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
18
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

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

×