MongoDB管理练习
一、索引
1.插入10W条数据 文档内容为:{name:zs-i,age:1}
2016-06-07T14:35:57.041+0800 I CONTROL [initandlisten]
> for(var i=0;i<100000;i++){
... db.person.insert({name:"zs"+i,age:i})
... }
WriteResult({ "nInserted" : 1 })
2.在属性name上创建索引
> db.person.ensureIndex({name:1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
3.查看执行计划,观察使用索引和不使用索引的区别
建立索引前
> db.person.find({name:"zs"+10000}).explain()
{
"cursor" : "BasicCursor",
"isMultiKey" : false,
"n" : 1,
"nscannedObjects" : 100000,
"nscanned" : 100000,
"nscannedObjectsAllPlans" : 100000,
"nscannedAllPlans" : 100000,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 781,
"nChunkSkips" : 0,
"millis" : 30,
"server" : "weekend01:27017",
"filterSet" : false
}
建索引后
> db.person.find({name:"zs"+10000}).explain()
{
"cursor" : "BtreeCursor name_1",
"isMultiKey" : false,
"n" : 1,
"nscannedObjects" : 1,
"nscanned" : 1,
"nscannedObjectsAllPlans" : 1,
"nscannedAllPlans" : 1,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"name" : [
[
"zs10000",
"zs10000"
]
]
},
"server" : "weekend01:27017",
"filterSet" : false
}
> db.person.find({name:"zs"+10000}).hint({name:1}).explain()
{
"cursor" : "BtreeCursor name_1",
"isMultiKey" : false,
"n" : 1,
"nscannedObjects" : 1,
"nscanned" : 1,
"nscannedObjectsAllPlans" : 1,
"nscannedAllPlans" : 1,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"name" : [
[
"zs10000",
"zs10000"
]
]
},
"server" : "weekend01:27017",
"filterSet" : false
}
二、备份与恢复
1.中断备份与恢复
a.备份数据库test中的student
[root@weekend01 ~]# mongoexport -d test -c student -o studnet.json
connected to: 127.0.0.1
exported 0 records
b.删除数据库test中的student
> db.student.drop()
true
c.恢复数据库test中的student
导入其他数据
[root@weekend01 ~]# mongoimport --db test --collection student --file studnet.json
connected to: 127.0.0.1
2016-06-07T18:02:18.612+0800 check 0 0
2016-06-07T18:02:18.612+0800 imported 0 objects
2.运行时备份与恢复、
a.备份数据库test
[root@weekend01 ~]# mongodump --host 127.0.0.1:27017 -d test -o /tmp
connected to: 127.0.0.1:27017
2016-06-07T18:04:27.161+0800 DATABASE: test to /tmp/test
2016-06-07T18:04:27.167+0800 test.system.indexes to /tmp/test/system.indexes.bson
2016-06-07T18:04:27.167+0800 2 documents
2016-06-07T18:04:27.167+0800 test.person to /tmp/test/person.bson
2016-06-07T18:04:27.482+0800 100000 documents
2016-06-07T18:04:27.482+0800 Metadata for test.person to /tmp/test/person.metadata.json
b.删除数据库test
> db.dropDatabase()
{ "dropped" : "test", "ok" : 1 }
c.恢复数据库test
[root@weekend01 ~]# mongorestore --host 127.0.0.1:27017 -d test -directoryperdb /tmp/test/
connected to: 127.0.0.1:27017
2016-06-07T18:06:22.060+0800 /tmp/test/person.bson
2016-06-07T18:06:22.060+0800 going into namespace [test.person]
100000 objects found
2016-06-07T18:06:23.253+0800 Creating index: { key: { _id: 1 }, name: "_id_", ns: "test.person" }
2016-06-07T18:06:23.297+0800 Creating index: { key: { name: 1 }, name: "name_1", ns: "test.person" }
主要算法及程序清单
a.插入10W条数据
for(var i=0;i<100000;i++){
db.person.insert({name:"zs"+i,age:i})
}
b.查询name为zs10000的数据
db.person.find({name:"zs"+10000}).explain() --看效果,此时没建name索引
c.建立索引
db.person.ensureIndex({name:1})
d.查询name为zs10000的数据
db.person.find({name:"zs"+10000}).explain()
db.person.find({name:"zs"+10000}).hint({name:1}).explain() --看效果,此时已建name索引
1.中断操作 -- 像冷备
a.把数据库test中的student导出 --备份
mongoexport -d test -c student -o d:/student.json
mongoexport --db test --collection student --out /root/student1.json
b.删除test数据库中的student
db.student.drop()
c.导入数据(中断其他操作) --恢复
mongoimport --db test --collection student --file /root/student.json
mongoimport -d test -c student --file d:/student.json
2.运行时备份 --像热备 整库备份
a.导出数据库
mongodump --host 127.0.0.1:27017 -d test -o /root
b.删除整个数据库
db.dropDatabase()
c.运行时恢复
mongorestore --host 127.0.0.1:27017 -d test -directoryperdb /tmp/test
疑难小结
本次实验重在考察对mongoDB的操作,类似上个实验,本次实验中难题没有,都是些基本的操作,只要在明白其理论基础上加上多敲多练多想就会有很大收获。看着再简单,不动手永远不知道自己会碰到什么状况,所以还是要多动手练习。
posted on 2017-07-27 19:00 Lucky_7 阅读(...) 评论(...) 编辑 收藏