MongoDB的使用

释放双眼,带上耳机,听听看~!

今天来学习一个新的数据库,叫做MongoDB数据库,我们先来了解一下MongoDB数据库的概念,再一起学习如何使用MongoDB数据库吧~

1.MongoDB的概念

MongoDB的使用

  • MongoDB是专为可扩展性、高性能和高可用性而设计的数据库,MongoDB的库中由一个或多个collections组成,这里的collection相当于关系型数据库中的表;
  • MongoDB中的记录是一个document文档,它是由字段和值对组成的数据结构,MongoDB文档类似于JSON对象,字段的值可以包括其他文档,数组和文档数组;
  • MongoDB支持的数据类型有:Int、Double, String, Object, Array, Binary data, Undefined, Boolean, Date, Null 等;

MongoDB的命令介绍

  • db.help() 查看库级别的命令
  • db.mycoll.help() 查看collection级别的命令
  • sh.help() 查看发片的命令
  • rs.help() 查看副本集的命令
  • help admin 查看管理相关的命令
  • help connect 查看连接到数据库的命令
  • help keys keys的相关命令
  • help misc misc things to know
  • help mr 查看mapreduce相关的命令
  • show dbs 查看当前的数据库
  • show collections 查看数据库中所有的collections
  • show users 当前的数据库中有哪些用户
  • show profile 显示profile信息,显示性能评估工具
  • show logs 显示日志名信息
  • show log [name] 显示指定查看对应日志的信息
  • use <db_name> 进入某库,设定某库为当前库
  • db.foo.find() 列出当前collection中所有的document
  • db.foo.find( { a : 1 } ) 列出当前collection中a = 1的document
  • it result of the last line evaluated; use to further iterate
  • DBQuery.shellBatchSize = x 设置显示的item的行数
  • exit 退出Mongo shell

CRUD操作

  • db.students.insert():插入一条数据,默认会创建students表;
  • show collections:显示当前的表;
  • db.students.stats():显示students表的数据信息;
  • db.students.find():查询插入的各个字段;
  • db.students.count():查看students表中有多少个document;

Collection的简单查询过滤操作


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
1
2如果你依然在编程的世界里迷茫,
3不知道自己的未来规划,
4对python感兴趣,
5这里推荐一下我的学习交流圈QQ群:895 797 751,
6里面都是学习python的,
7
8db.students.remove({&quot;name&quot;: &quot;Angle&quot;})
9
10# 删除表
11db.students.drop()
12
13# 删除当前数据库
14db.dropDatabase()
15
16
17find()的高级用法
18比较操作:
19
20
21$gt:大于;
22
23db.students.find({age: {$gt: 10}})  age大于10
24
25
26
27$gte:大于等于;
28
29db.students.find({age: {$gte: 20}})  age大于等于20
30
31
32
33$lt:小于;
34
35db.students.find({age: {$lt: 30}})  age小于30
36
37
38
39$lte:小于等于;
40
41db.students.find({age: {$lte: 40}})  age小于等于40
42
43
44
45$in:在范围内;
46
47db.students.find({age: {$in: [10, 20]}})  age在[10, 20]的document
48
49
50
51$nin:不在范围内;
52
53db.students.find({age: {$nin: [30, 40]}})  age不在[30, 40]的document
54
55

逻辑运算:


1
2
3
4
5
6
7
8
9
10
11
1
2$or:或运算;
3
4db.students.find({$or: [{name: {$eq: &quot;robby&quot;}}, {age: {$nin: [40,50]}}]})
5
6
7$and:与运算;
8$not:非运算;
9$nor:取反运算;
10
11

元素查询:

  • $exists:查询存在某字段的document;

  • 如查询存在name字段的document

db.students.find({name: {$exists: true}})

  • $mod:取摸; *$type:返回指定字段的值类型为指定类型的document;

4.update()的高级用法

$set:更新,或插入字段的值;

  • 将name为Tom的这个document的age字段的值改为20

db.students.update({name: "Tom"}, {$set: {age: 20}})

$unset:删除指定字段;

  • 删除name字段为Tom的document的age为25的字段

db.students.update({name: "Tom"}, {$unset: {age: 25}}

$rename:修改字段名;

  • 给name字段为Tom的document增加一个字段sex且值为男

1
1`

db.students.update({name: “Tom”}, {$inc: {sex: “男”}})

createUser()方法


1
2
3
4
5
6
7
8
1# 创建用户
2db.createUser({user:&quot;root&quot;,pwd:&quot;123456&quot;, roles: [{ role: &quot;root&quot;, db: &quot;admin&quot; }]});
3
4# 删除用户
5db.system.users.remove({user:&quot;root&quot;});  
6
7
8
  • 方法中的user用于指定用户名、pwd用于设置密码、roles用于指定用户的角色,可以用一个空数组给新用户设定空角色、db用于指定用户对哪个数据库具有管理员权限;
  • createUser()方法为数据库创建新用户,如果用户已存在于数据库中,则db.createUser()返回重复的用户错误;

角色种类说明

  • 数据库用户角色:read(允许用户读取指定数据库)、readWrite(允许用户读写指定数据库 );
  • 数据库管理角色:dbAdmin(允许用户在指定数据库中执行管理函数)、dbOwner、userAdmin(允许用户向system.users集合写入);
  • 集群管理角色:clusterAdmin(赋予用户所有分片和复制集相关函数的管理权限)、clusterManager、clusterMonitor、hostManage
  • 备份恢复角色:backup、restore;
  • 所有数据库角色:readAnyDatabase、readWriteAnyDatabase(赋予用户所有数据库的读权限 )、userAdminAnyDatabase(赋予* 用户所有数据库的读写权限 )、dbAdminAnyDatabase(赋予用户所有数据库的dbAdmin权限);
  • 超级用户角色:root(超级账号,超级权限);
  • 内部角色:__system;

Mongodb Index 介绍

  • MongoDB中的索引与MySQL中的索引有类似的功能,将表中的字段添加索引,索引会将字段做排序,依次索引能够大大提高*MongoDB的查询能力
  • 索引是特殊的数据结构,它以易于遍历的形式存储集合数据集的一小部分,索引存储特定字段或字段集的值,按字段值排序;
  • 索引条目的排序支持有效的等式匹配和基于范围的查询操作;
  • MongoDB可以使用索引中的顺序返回排序结果;

db.students.createIndex({name: 1})

  • 给name字段创建索引, 1为指定按升序创建索引,如果你想按降序来创建索引指定为-1即可


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
1# 查看索引
2db.students.getIndexes()
3
4# 查看是否使用到了索引(由于MongoDB调优)
5db.students.find({&quot;name&quot;: &quot;Angle&quot;}).explain()  # winningPlan的stage为fetch,非COLLSCAN(字段扫描)
6
7# 删除索引
8db.student.dropIndex(&quot;name_1&quot;)
9
10# 给name字段创建一个唯一键索引,那么再给students表增加一条行document,且name与之前存在的document的name值相同,那么就会报错, 如:增加一条document
11db.students.createIndex({name: 1}, {unique: true})
12db.students.insert({name: &quot;Angle&quot;})
13
14报错的信息如下:
15WriteResult({
16        &quot;nInserted&quot; : 0,
17        &quot;writeError&quot; : {
18                &quot;code&quot; : 11000,
19                &quot;errmsg&quot; : &quot;insertDocument :: caused by :: 11000 E11000 duplicate key error index: test.students.$name_1  dup key: { : \&quot;Angle\&quot; }&quot;
20        }
21})
22如果你依然在编程的世界里迷茫,
23不知道自己的未来规划,
24对python感兴趣,
25这里推荐一下我的学习交流圈QQ群:895 797 751,
26里面都是学习python的,
27
28

给TA打赏
共{{data.count}}人
人已打赏
安全运维

OpenSSH-8.7p1离线升级修复安全漏洞

2021-10-23 10:13:25

安全运维

设计模式的设计原则

2021-12-12 17:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索