MongoDB索引与备份

263 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 17 天,点击查看活动详情

六、索引

1、创建索引

索引:以提升查询速度

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

> for(i=0;i<100000;i++){db.test005.insert({name:'test'+i,age:i})}
WriteResult({ "nInserted" : 1 })
> db.test005.find().count()
100000

查询运行时间语句后面+.explain('executionStats')

//案例
> db.test005.find({name:'test10000'}).explain('executionStats')

建立索引语法db.集合名称.ensureIndex({字段名:1});其中 1 表示升序, -1 表示降序

建立索引之后对比

> db.test005.ensureIndex({name:1})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}
> db.test005.find({name:'test10000'}).explain('executionStats')

在这里插入图片描述

2、索引操作

查看当前集合的所有索引

> db.test005.getIndexes()
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_"
        },
        {
                "v" : 2,
                "key" : {
                        "name" : 1
                },
                "name" : "name_1"
        }
]

删除索引:db.集合名称.dropIndex({'索引名称'})

> db.test005.dropIndex({'name':1})
{ "nIndexesWas" : 2, "ok" : 1 }
//再次查看全部索引
> db.test005.getIndexes()
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" } ]

创建唯一索引(索引的值是唯一的;在默认情况下索引字段的值可以相同):

> db.test005.ensureIndex({"name":1}, {"unique":true})
{
        "ok" : 0,
        "errmsg" : "Index build failed: eab854cd-330b-414f-ae86-9cfb317efbf5: Collection test.test005 ( 45744ab1-2a71-4722-8f84-99812ccc9ffb ) :: caused by :: E11000 duplicate key error collection: test.test005 index: name_1 dup key: { name: \"test0\" }",
        "code" : 11000,
        "codeName" : "DuplicateKey",
        "keyPattern" : {
                "name" : 1
        },
        "keyValue" : {
                "name" : "test0"
        }
}

建立联合索引(什么时候需要联合索引)

> db.test005.ensureIndex({name:1, age:1})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}
> db.test005.getIndexes()
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_"
        },
        {
                "v" : 2,
                "key" : {
                        "name" : 1,
                        "age" : 1
                },
                "name" : "name_1_age_1"
        }
]

七、备份和恢复

1、备份

备份的语法mongodump -h dbhost -d dbname -o dbdirectory

  • -h: 指定服务器地址;如果是当前本机数据库可以去掉-h
  • -port :指定端口号;如果是默认端口可以去掉
  • -d: 需要备份的数据库名称;如果不指定则导出所有数据库
  • -o: 备份的数据存放位置, 此⽬录中存放着备份出来的数据
  • -c: 指定集合名称;如果不指定则全部导出
  • -u: 用户名;如果没有用户,可以不用指定
  • -p: 密码;如果没有密码,可以不用指定

注意

  • 命名需要在终端输出,而不是数据库命令行
  • 每个参数前后是有空格

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

案例:

//保存本地数据库中test库在桌面
mongodump  -d test -o ~C:\Users\ym\Desktop\

2、恢复

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

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

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