# 2.4. Mongdb的索引备份以及和python交互

# 目标

  • 理解mongodb创建索引的目的
  • 掌握mongdb创建索引的方式
  • 熟悉mongdb备份和恢复的命令

# 1. mongodb的索引

知识点

  • 掌握mongodb索引的创建,删除操作
  • 掌握mongodb查看索引的方法
  • 掌握mongodb创建联合索引的方法
  • 掌握mongodb创建唯一索引的方法

# 1.1 为什么mongdb需要创建索引

  • 加快查询速度
  • 进行数据的去重

# 1.2 mongodb创建简单的索引方法

  • 语法:
    • db.集合.ensureIndex({属性:1}),1表示升序, -1表示降序
    • db.集合.createIndex({属性:1})
    • 上面两个命令效果等价
  • 具体操作:db.db_name.ensureIndex({name:1})

# 1.3 创建索引前后查询速度对比

测试:插入10万条数据到数据库中 插入数据:

for(i=0;i<100000;i++){db.t255.insert({name:'test'+i,age:i})}
1

创建索引前:

db.t1.find({name:'test10000'})
db.t1.find({name:'test10000'}).explain('executionStats')
1
2

创建索引后:

db.t255.ensureIndex({name:1})
db.t1.find({name:'test10000'}).explain('executionStats')
1
2

前后速度对比

xcooo

# 1.4 索引的查看

默认情况下_id是集合的索引

查看方式:db.collection_name.getIndexes()

添加索引前:

> db.test2000.insert({"name":"hello",age:20})
WriteResult({ "nInserted" : 1 })
> db.test2000.find()
{ "_id" : ObjectId("5ae0232f625b9ddd91a0e7ae"), "name" : "hello", "age" : 20 }
> db.test2000.getIndexes()
[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test2000.test2000"
    }
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

添加name为索引后:

> db.test2000.ensureIndex({name:1})
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 2,
    "ok" : 1
}
> db.test2000.getIndexes()
[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test2000.test2000"
    },
    {
        "v" : 2,
        "key" : {
            "name" : 1
        },
        "name" : "name_1",
        "ns" : "test2000.test2000"
    }
]
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

# 1.5 mongodb创建唯一索引

在默认情况下mongdb的索引字段的值是可以相同的,仅仅能够提高查询速度

添加唯一索引的语法:

db.collection_name.ensureIndex({"name":1},{"unique":true})
1

使用普通索引的效果如下:

> db.test2000.getIndexes()
[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test2000.test2000"
    },
    {
        "v" : 2,
        "key" : {
            "name" : 1
        },
        "name" : "name_1",
        "ns" : "test2000.test2000"
    }
]
> db.test2000.insert({name:"hello",age:40})
WriteResult({ "nInserted" : 1 })
> db.test2000.find()
{ "_id" : ObjectId("5ae0232f625b9ddd91a0e7ae"), "name" : "hello", "age" : 20 }
{ "_id" : ObjectId("5ae02421625b9ddd91a0e7af"), "name" : "hello", "age" : 30 }
{ "_id" : ObjectId("5ae02432625b9ddd91a0e7b0"), "name" : "hello", "age" : 40 }
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

添加age为唯一索引之后:

> db.test2000.createIndex({age:1},{unique:true})
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 2,
    "numIndexesAfter" : 3,
    "ok" : 1
}
> db.test2000.getIndexes()
[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test2000.test2000"
    },
    {
        "v" : 2,
        "key" : {
            "name" : 1
        },
        "name" : "name_1",
        "ns" : "test2000.test2000"
    },
    {
        "v" : 2,
        "unique" : true,
        "key" : {
            "age" : 1
        },
        "name" : "age_1",
        "ns" : "test2000.test2000"
    }
]
> db.test2000.insert({"name":"world",age:20})
WriteResult({
    "nInserted" : 0,
    "writeError" : {
        "code" : 11000,
        "errmsg" : "E11000 duplicate key error collection: test2000.test2000 index: age_1 dup key: { : 20.0 }"
    }
})
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

# 1.6 删除索引

语法:db.t1.dropIndex({'索引名称':1})

> db.test2000.getIndexes()
[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test2000.test2000"
    },
    {
        "v" : 2,
        "key" : {
            "name" : 1
        },
        "name" : "name_1",
        "ns" : "test2000.test2000"
    },
    {
        "v" : 2,
        "unique" : true,
        "key" : {
            "age" : 1
        },
        "name" : "age_1",
        "ns" : "test2000.test2000"
    }
]
> db.test2000.dropIndex({age:1})
{ "nIndexesWas" : 3, "ok" : 1 }
> db.test2000.dropIndex({name:1})
{ "nIndexesWas" : 2, "ok" : 1 }
> db.test2000.getIndexes()
[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test2000.test2000"
    }
]
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

# 1.6 建立复合索引

在进行数据去重的时候,可能用一个字段来保证数据的唯一性,这个时候可以考虑建立复合索引来实现。

例如:抓全贴吧信息,如果把帖子的名字作为唯一索引对数据进行去重是不可取的,因为可能有很多帖子名字相同

建立复合索引的语法:db.collection_name.ensureIndex({字段1:1,字段2:1})

> db.test2000.getIndexes()
[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test2000.test2000"
    }
]
> db.test2000.createIndex({name:1,age:1})
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 2,
    "ok" : 1
}
> db.test2000.getIndexes()
[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test2000.test2000"
    },
    {
        "v" : 2,
        "key" : {
            "name" : 1,
            "age" : 1
        },
        "name" : "name_1_age_1",
        "ns" : "test2000.test2000"
    }
]
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

# 1.7 建立索引注意点

  • 根据需要选择是否需要建立唯一索引

  • 索引字段是升序还是降序在单个索引的情况下不影响查询效率,但是带复合索引的条件下会有影响

    例如:在进行查询的时候如果字段1需要升序的方式排序输出,字段2需要降序的方式排序输出,那么此时复合索引的建立需要把字段1设置为1,字段2设置为-1

# 2. mongodb的备份和恢复

知识点

  • 掌握mongdb的备份和恢复命令

# 2.1 备份

备份的语法:

mongodump -h dbhost -d dbname -o dbdirectory
1
  • -h: 服务器地址, 也可以指定端⼝号

  • -d: 需要备份的数据库名称

  • -o: 备份的数据存放位置, 此⽬录中存放着备份出来的数据

示例:mongodump -h 192.168.196.128:27017 -d test1 -o ~/Desktop/test1bak

# 2.2 恢复

恢复语法:mongorestore -h dbhost -d dbname --dir dbdirectory

  • -h: 服务器地址
  • -d: 需要恢复的数据库实例
  • --dir: 备份数据所在位置

示例:mongorestore -h 192.168.196.128:27017 -d test2 --dir ~/Desktop/test1bak/test1

# 3. 动手

  1. 尝试将我电脑中的douban.tv1中的数据恢复到自己的电脑中,具体如何操作?

  2. 完成上述操作后完成以下问题:

    2.1.获取每条数据中的title,count(所有评分人数),rate(评分),country(国家)的这些字段

    2.2.获取上述结果中的不同国家电视剧的数据量

    2.3.获取上述结果中分数大于8分的不同国家电视剧的数据量

# 小结

  • 本小结重点
    • 掌握mongodb中创建索引的方法
    • 掌握mongodb中查看和删除索引的方法
    • 掌握mongodb中创建唯一索引的方法
    • 掌握mongodb中创建联合索引的方法
    • 掌握mongodb中数据的备份和恢复的方法
上次更新: 2020/10/15 下午8:49:48