MongoDB权威指南<2> 1-1 MongoDB 介绍
Char01 MongoDB简介
1.1 易于使用
是面向文档document-oriented的数据库
1.2 易于扩展
MongoDB的设计采用横向扩展,如果一个集群需要更大的容量,只需要向集群添加新的服务器,MongoDB 就会自动将现有的数据像服务器传送
1.3 丰富的功能
索引 indexing
聚合 aggregation
特殊的集合类型
文档存储
1.4 卓越的性能
Char02 MongoDB基础知识
2.1 文档 -- 行
文档被表示为对象
键可以使用任意UTF-8字符
MongoDB不但区分类型,而且区分大小写
MongoDB文档不能有重复的键
{"greeting":"hello ... "}
2.2 集合 -- 表 【一组文档】
2.2.1 动态模式:
集合是动态模式
2.2.2 命名
2.3 数据库
数据库名应该全部小写,
数据库最终会变成文件系统里的文件,而数据库名就是相应的文件名
2.4 启动 MongoDB
默认端口好28017
2.5 MongoDB Shell 简介
2.5.1 MongoDB自带JavaScript shell
2.5.2 MongoDB 客户端
2.5.3 Shell 操作
CRUD : create , read , update, delete
# mongo MongoDB shell version: 3.0.14 connecting to: test 2017-01-19T15:55:15.814+0800 W NETWORK Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused 2017-01-19T15:55:15.814+0800 E QUERY Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at connect (src/mongo/shell/mongo.js:179:14) at (connect):1:6 at src/mongo/shell/mongo.js:179 exception: connect failed root@Ly-banya:~# root@Ly-banya:~# mongo MongoDB shell version: 3.0.14 connecting to: test 2017-01-19T15:55:28.999+0800 W NETWORK Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused 2017-01-19T15:55:29.000+0800 E QUERY Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at connect (src/mongo/shell/mongo.js:179:14) at (connect):1:6 at src/mongo/shell/mongo.js:179 exception: connect failed root@Ly-banya:~# ls ls lsattr lsblk lsb_release lscpu lsdiff lshw lsinitramfs lsmod lsof lspci lspcmcia lspgpot lss16toppm lsusb root@Ly-banya:~# lsof -i:27017 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mongod 25779 mongodb 6u IPv4 28934926 0t0 TCP localhost:27017 (LISTEN) root@Ly-banya:~# ^Cof -i:27017 root@Ly-banya:~# kill -9 ^C root@Ly-banya:~# exit ly@Ly-banya:~$ mongo MongoDB shell version: 3.0.14 connecting to: test Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user Server has startup warnings: 2017-01-19T15:55:44.532+0800 I CONTROL [initandlisten] 2017-01-19T15:55:44.532+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2017-01-19T15:55:44.532+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2017-01-19T15:55:44.532+0800 I CONTROL [initandlisten] 2017-01-19T15:55:44.532+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2017-01-19T15:55:44.532+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2017-01-19T15:55:44.532+0800 I CONTROL [initandlisten] > > db test > use foobar switched to db foobar > db foobar > post={"title":"Blog Post", ... "content":"This is Content", ... "date":new Date()} { "title" : "Blog Post", "content" : "This is Content", "date" : ISODate("2017-01-19T07:57:57.398Z") } > db.blog.find() > db.blog.insert(post) WriteResult({ "nInserted" : 1 }) > db.blog.find() { "_id" : ObjectId("5880719675f14a9225e239f8"), "title" : "Blog Post", "content" : "This is Content", "date" : ISODate("2017-01-19T07:57:57.398Z") } > post.comments = [] [ ] > db.blog.update({"title":"Blog Post"} , post) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.blog.find() { "_id" : ObjectId("5880719675f14a9225e239f8"), "title" : "Blog Post", "content" : "This is Content", "date" : ISODate("2017-01-19T07:57:57.398Z"), "comments" : [ ] } > > db.log.remove({"title":"Blog Post"}) WriteResult({ "nRemoved" : 0 }) > db.blog.find() { "_id" : ObjectId("5880719675f14a9225e239f8"), "title" : "Blog Post", "content" : "This is Content", "date" : ISODate("2017-01-19T07:57:57.398Z"), "comments" : [ ] } > db.blog.remove({"title":"Blog Post"}) > db.blog.remove({"title":"Blog Post"}) WriteResult({ "nRemoved" : 1 }) > db.blog.find()
2.6 数据类型
2.6.1 基本数据类型
2.6.2 日期
2.6.4 内嵌文档
2.6.5 _id && ObjectId
2.7 使用 MongoDB shell