MongoDB学习

一、MongoDB快速入门

RDBMS VS MongoDB

直接给出的表显示RDBMS(关系型数据库管理系统)术语 与 MongoDB 的关系,直观明了:

SQL术语/概念 MongoDB术语/概念 解释/说明
database database 数据库
table collection 数据库表/集合
row document 数据记录行/文档
column field 数据属性/字段(域)
index index 索引
table joins Embedded Documents 表连接,MongoDB3.2提供了Join操作
primary key primary key 主键,MongoDB默认自动将_id字段设置为主键,可以手动设置

通过下图实例,也可以更直观的的了解Mongo中的一些概念:

image.png

二、MongoDB基本操作

Mongo自带shell客户端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
$ mongo

> show dbs show database names

> db
查看当前数据库的名称

> show collections
显示当前数据库的所有集合

> use foolbar
切换到foolbar数据库,如果foolbar数据库不存在,会在该数据第一次插入之后创建

> show collections;
显示当前数据库中有哪些集合,起初为空

>db.blog.insert({"title1":"instrduce of mongo"});
WriteResult({ "nInserted" : 1 })
第一次插入数据会创建数据库(集合) 插入的数据是一个文档

> db.blog.find()
{ "_id" : ObjectId("5503ecd83853a67e204962ae"), "title1" : "instrduce of mongo" }
查找当前数据库中blog集合的所有文档

> db.blog.insert({"docment":"hello mongoDB!"});
WriteResult({ "nInserted" : 1 })

> db.blog.find()
{ "_id" : ObjectId("5503ecd83853a67e204962ae"), "title1" : "instrduce of mongo" }
{ "_id" : ObjectId("5503ed873853a67e204962af"), "docment" : "hello mongoDB!" }

> db.blog.insert({"title" : "mytest", "name" : "pc"});
WriteResult({ "nInserted" : 1 })

> db.blog.insert({"title" : "mytest", "name" : "pc"});
WriteResult({ "nInserted" : 1 })

> db.blog.insert({"name" : "xwp"});
WriteResult({ "nInserted" : 1 })

> db.blog.insert({"name" : "xwp"});
WriteResult({ "nInserted" : 1 })

> db.blog.find()
{ "_id" : ObjectId("550247c18976c0e0b467e800"), "title" : "mytest", "name" : "pc" }
{ "_id" : ObjectId("550248468976c0e0b467e801"), "name" : "xwp" }
{ "_id" : ObjectId("5503ecd83853a67e204962ae"), "title1" : "instrduce of mongo" }
{ "_id" : ObjectId("5503ed873853a67e204962af"), "docment" : "hello mongoDB!" }
{ "_id" : ObjectId("5503edee3853a67e204962b0"), "title" : "mytest", "name" : "pc" }
{ "_id" : ObjectId("5503ee013853a67e204962b1"), "name" : "xwp" }

> db.blog.remove({_id : ObjectId("550247c18976c0e0b467e800")});
WriteResult({ "nRemoved" : 1 })
从当前数据库中的blog集合中移除_id为ObjectId("550247c18976c0e0b467e800")的文档

> db.blog.find()
{ "_id" : ObjectId("550248468976c0e0b467e801"), "name" : "xwp" }
{ "_id" : ObjectId("5503ecd83853a67e204962ae"), "title1" : "instrduce of mongo" }
{ "_id" : ObjectId("5503ed873853a67e204962af"), "docment" : "hello mongoDB!" }
{ "_id" : ObjectId("5503edee3853a67e204962b0"), "title" : "mytest", "name" : "pc" }
{ "_id" : ObjectId("5503ee013853a67e204962b1"), "name" : "xwp" }


> show collections;
blog

> db.createCollection("xunlei");
{ "ok" : 1 }
在当前数据库创建一个指定名称的集合

> show collections;
blog
xunlei

>db.blog.findOne()
{ "_id" : ObjectId("550248468976c0e0b467e801"), "name" : "xwp" }
这个shell函数findOne会返回一个文档 而find函数会返回最多二十个文档.更多区别我们在后面详细介绍.

>db.blog.count()
5
统计集合blog所有文档数量

ctrl+c退出mongo客户端

最基本操作上述可见,补充一些:

  • pretty() 方法

结果显示在一个格式化的方式,可以使用 pretty() 方法.

语法:

1
>db.mycol.find().pretty()
  • MongoDB中AND

语法:

在 find() 方法,如果通过多个键分离’,’,那么 MongoDB 处理 AND 条件。AND 基本语法如下所示:

1
>db.mycol.find({key1:value1, key2:value2}).pretty()
  • MongoDB中OR

语法:

OR条件的基础上要查询文件,需要使用$or关键字。OR 基本语法如下所示:

1
2
3
4
5
6
7
>db.mycol.find(
{
$or: [
{key1: value1}, {key2:value2}
]
}
).pretty()
  • MongoDB 与 RDBMS Where 语句比较
操作 语法 例子 RDBMS 等同
Equality {< key>:< value>} db.mycol.find({“by”:”tutorials itcast”}).pretty() where by = ‘tutorials itcast’
Less Than {< key>:{$lt:< value>}} db.mycol.find({“likes”:{$lt:50}}).pretty() where likes < 50
Less Than Equals {< key>:{$lte:< value>}} db.mycol.find({“likes”:{$lte:50}}).pretty() where likes <= 50
Greater Than {< key>:{$gt:< value>}} db.mycol.find({“likes”:{$gt:50}}).pretty() where likes > 50
Greater Than Equals {< key>:{$gte:< value>}} db.mycol.find({“likes”:{$gte:50}}).pretty() where likes >= 50
Not Equals {< key>:{$ne:< value>}} db.mycol.find({“likes”:{$ne:50}}).pretty() where likes != 50
  • 更新操作 在 MongoDB update() 和 SQL Update 区别
SQL Update Statements MongoDB update() Statements
UPDATE users SET status = "C" WHERE age > 25 db.users.update( { age: { $gt: 25 } }, { $set: { status: "C" } }, { multi: true } )
UPDATE users SET age = age + 3 WHERE status = "A" db.users.update( { status: "A" } , { $inc: { age: 3 } }, { multi: true } )
  • 在执行remove()函数前先执行find()命令来判断执行的条件是否正确,这是一个比较好的习惯。remove命令一定要三思而后行!!

:转载文章请注明出处,谢谢~