M102: MongoDB for DBAs chapter 3 performance学习记录
运行环境
操作系统:windows 10 家庭中文版
Mongodb :Mongodb 3.4
Mongodb安装路径:E:>MongoDB\Server\3.4\bin\
Mongodb存储路径:E:>MongoDB\data
课后习题
3.1题目
Download Handouts:
performance__m102_performance_reorg_537a56c08bb48b7c467a20d3.zip
Start a mongod server instance (if you still have a replica set, that would work too).
Next, download the handout and run:
mongo --shell localhost/performance performance.js
homework.init()
Build an index on the “active” and “tstamp” fields. You can verify that you’ve done your job with
db.sensor_readings.getIndexes()
When you are done, run:
homework.a()
and enter the numeric result below (no spaces).
Note: if you would like to try different indexes, you can use db.sensor_readings.dropIndexes() to drop your old index before creating a new one. (For this problem you will only need one index beyond the _id index which is present by default.)
解答
下载指定的performance__m102_performance_reorg_537a56c08bb48b7c467a20d3.zip文件,解压后放在E:\MongoDB目录下,启动我的mongod服务:
C:\Users\Shinelon>e:
E:\>MongoDB\Server\3.4\bin\mongod.exe --dbpath MongoDB\data
按要求通过js脚本启动mongo数据库
E:\>MongoDB\Server\3.4\bin\mongo.exe --shell localhost/performance MongoDB\performance.js
MongoDB shell version v3.4.6
connecting to: mongodb://localhost/performance
MongoDB server version: 3.4.6
type "help" for help
Server has startup warnings:
2018-04-08T20:18:31.157-0700 I CONTROL [initandlisten]
2018-04-08T20:18:31.157-0700 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-08T20:18:31.157-0700 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-04-08T20:18:31.158-0700 I CONTROL [initandlisten]
>
初始化homework脚本:
> homework.init()
{
"connectionId" : 2,
"n" : 0,
"syncMillis" : 0,
"writtenTo" : null,
"err" : null,
"ok" : 1
}
still working...
{
"connectionId" : 2,
"updatedExisting" : true,
"n" : 20000,
"syncMillis" : 0,
"writtenTo" : null,
"err" : null,
"ok" : 1
}
count: 20000
先查看sensor_readings集合中索引情况:
> db.sensor_readings.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "performance.sensor_readings"
}
]
结果显示只在_id字段上有一个默认的索引,下面按题目要求在”active” 和”tstamp”字段创建一个复合索引:
> db.sensor_readings.ensureIndex({
"active" : 1 , "tstamp" : 1})
{
"