M102: MongoDB for DBAs chapter 1学习记录
2018-03-29 15:07
435 查看
M102: MongoDB for DBAs chapter 1
运行环境
操作系统:windows 10 家庭中文版Mongodb :Mongodb 3.4
Mongodb安装路径:E:>MongoDB\Server\3.4\bin\
Mongodb存储路径:E:>MongoDB\data
课后问题
1.1问题
Download and install MongoDB from www.mongodb.org. Then run the database as a single server instance on your PC (that is, run the mongod binary). Then, run the administrative shell.From the shell prompt type
db.isMaster().maxBsonObjectSize
at the “>” prompt.
What do you get as a result?
Enter answer here:
16777216
解答
mongodb安装方法见我之前的博客:CentOS7下使用yum安装Mongodb3.4
进入mongo
C:\Users\Shinelon>e: E:\>MongoDB\Server\3.4\bin\mongo.exe MongoDB shell version v3.4.6 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.4.6 Server has startup warnings: 2018-03-28T20:01:23.957-0700 I CONTROL [initandlisten] 2018-03-28T20:01:23.957-0700 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2018-03-28T20:01:23.958-0700 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2018-03-28T20:01:23.958-0700 I CONTROL [initandlisten]
执行所给命令
> db.isMaster().maxBsonObjectSize 16777216
这其实是默认的文档大小,即16MB
1.2问题
Download Handouts:Products__hw1.2_m102_529e39a8e2d42347509fb3f0.json
Download the handout. Take a look at its content.
Now, import its contents into MongoDB, into a database called “pcat” and a collection called “products”. Use the mongoimport utility to do this.
When done, run this query in the mongo shell:
db.products.find( { type : "case" } ).count()
What’s the result?
3
解答
导入所给的Products__hw1.2_m102_529e39a8e2d42347509fb3f0.json文件C:\Users\Shinelon>e: E:\>MongoDB\Server\3.4\bin\mongoimport.exe --drop -d pcat -c products C:\Users\Shinelon\Downloads\Products__hw1.2_m102_529e39a8e2d42347509fb3f0.json
进入mongo,并进入pcat库
E:\>MongoDB\Server\3.4\bin\mongo.exe MongoDB shell version v3.4.6 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.4.6 Server has startup warnings: 2018-03-28T20:01:23.957-0700 I CONTROL [initandlisten] 2018-03-28T20:01:23.957-0700 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2018-03-28T20:01:23.958-0700 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2018-03-28T20:01:23.958-0700 I CONTROL [initandlisten] > use pcat switched to db pcat
执行所给查询命令求出结果:
> db.products.find( { type : "case" } ).count() 3
1.3问题
At this point you should have pcat.products loaded from the previous step. You can confirm this by running in the shell:db.products.find() // or: db.products.count() // should print out "11"
Now, what query would you run to get all the products where brand equals the string “ACME”?
Enter answer here:
db.produ 4000 cts.find({brand:"ACME"})
解答
验证数据库导入正常:> db.products.find() { "_id" : "ac3", "name" : "AC3 Phone", "brand" : "ACME", "type" : "phone", "price" : 200, "warranty_years" : 1, "available" : true } { "_id" : "ac7", "name" : "AC7 Phone", "brand" : "ACME", "type" : "phone", "price" : 320, "warranty_years" : 1, "available" : false } { "_id" : ObjectId("507d95d5719dbef170f15c00"), "name" : "Phone Service Family Plan", "type" : "service", "monthly_price" : 90, "limits" : { "voice" : { "units" : "minutes", "n" : 1200, "over_rate" : 0.05 }, "data" : { "n" : "unlimited", "over_rate" : 0 }, "sms" : { "n" : "unlimited", "over_rate" : 0.01 } }, "sales_tax" : true, "term_years" : 3, "over_rate" : 0 } { "_id" : ObjectId("507d95d5719dbef170f15bff"), "name" : "Phone Service Core Plan", "type" : "service", "monthly_price" : 60, "limits" : { "voice" : { "units" : "minutes", "n" : 1000, "over_rate" : 0.05 }, "data" : { "n" : "unlimited", "over_rate" : 0 }, "sms" : { "n" : "unlimited", "over_rate" : 0 } }, "term_years" : 1 } { "_id" : ObjectId("507d95d5719dbef170f15bfb"), "name" : "Phone Extended Warranty", "type" : "warranty", "price" : 38, "warranty_years" : 2, "for" : [ "ac3", "ac7", "ac9", "qp7", "qp8", "qp9" ] } { "_id" : ObjectId("507d95d5719dbef170f15bf9"), "name" : "AC3 Series Charger", "type" : [ "accessory", "charger" ], "price" : 19, "warranty_years" : 0.25, "for" : [ "ac3", "ac7", "ac9" ] } { "_id" : ObjectId("507d95d5719dbef170f15c01"), "name" : "Cable TV Basic Service Package", "type" : "tv", "monthly_price" : 50, "term_years" : 2, "cancel_penalty" : 25, "sales_tax" : true, "additional_tarriffs" : [ { "kind" : "federal tarriff", "amount" : { "percent_of_service" : 0.06 } }, { "kind" : "misc tarriff", "amount" : 2.25 } ] } { "_id" : ObjectId("507d95d5719dbef170f15bfc"), "name" : "AC3 Case Black", "type" : [ "accessory", "case" ], "color" : "black", "price" : 12.5, "warranty_years" : 0.25, "available" : false, "for" : "ac3" } { "_id" : ObjectId("507d95d5719dbef170f15bfa"), "name" : "AC3 Case Green", "type" : [ "accessory", "case" ], "color" : "green", "price" : 12, "warranty_years" : 0 } { "_id" : ObjectId("507d95d5719dbef170f15bfe"), "name" : "Phone Service Basic Plan", "type" : "service", "monthly_price" : 40, "limits" : { "voice" : { "units" : "minutes", "n" : 400, "over_rate" : 0.05 }, "data" : { "units" : "gigabytes", "n" : 20, "over_rate" : 1 }, "sms" : { "units" : "texts sent", "n" : 100, "over_rate" : 0.001 } }, "term_years" : 2 } { "_id" : ObjectId("507d95d5719dbef170f15bfd"), "name" : "AC3 Case Red", "type" : [ "accessory", "case" ], "color" : "red", "price" : 12, "warranty_years" : 0.25, "available" : true, "for" : "ac3" } > db.products.count() 11
编写并验证查询命令:
> db.products.find({brand:"ACME"}) { "_id" : "ac3", "name" : "AC3 Phone", "brand" : "ACME", "type" : "phone", "price" : 200, "warranty_years" : 1, "available" : true } { "_id" : "ac7", "name" : "AC7 Phone", "brand" : "ACME", "type" : "phone", "price" : 320, "warranty_years" : 1, "available" : false } { "_id" : "ac9", "name" : "AC9 Phone", "brand" : "ACME", "type" : "phone", "price" : 333, "warranty_years" : 0.25, "available" : true }
1.4问题
How would you print out, in the shell, just the value in the “name” field, for all the product documents in the collection, without extraneous characters or braces, sorted alphabetically, ascending? (Check all that would apply.)Check all that apply:
A
db.products.find( { }, { name : 1, _id : 0 } ).sort( { name : 1 } )
B
var c = db.products.find( { }, { name : 1, _id : 0 } ).sort( { name : 1 } ); while( c.hasNext() ) { print( c.next().name); }
C
var c = db.products.find( { } ).sort( { name : 1 } ); c.forEach( function( doc ) { print( doc.name ) } );
D
var c = db.products.find( { } ).sort( { name : -1 } ); while( c.hasNext() ) { print( c.next().name); }
答案:bc
解答
A> db.products.find( { }, { name : 1, _id : 0 } ).sort( { name : 1 } ) { "name" : "AC3 Case Black" } { "name" : "AC3 Case Green" } { "name" : "AC3 Case Red" } { "name" : "AC3 Phone" } { "name" : "AC3 Series Charger" } { "name" : "AC7 Phone" } { "name" : "Cable TV Basic Service Package" } { "name" : "Phone Extended Warranty" } { "name" : "Phone Service Basic Plan" } { "name" : "Phone Service Core Plan" } { "name" : "Phone Service Family Plan" }
查询除了所有的字段,且返回格式不符合要求
B
> var c = db.products.find( { }, { name : 1, _id : 0 } ).sort( { name : 1 } ); > while( c.hasNext() ) { ... print( c.next().name); ... } AC3 Case Black AC3 Case Green AC3 Case Red AC3 Phone AC3 Series Charger AC7 Phone Cable TV Basic Service Package Phone Extended Warranty Phone Service Basic Plan Phone Service Core Plan Phone Service Family Plan
符合要求
C
> var c = db.products.find( { } ).sort( { name : 1 } ); > c.forEach( function( doc ) { print( doc.name ) } ); AC3 Case Black AC3 Case Green AC3 Case Red AC3 Phone AC3 Series Charger AC7 Phone Cable TV Basic Service Package Phone Extended Warranty Phone Service Basic Plan Phone Service Core Plan Phone Service Family Plan
同样符合要求
D
> var c = db.products.find( { } ).sort( { name : -1 } ); > while( c.hasNext() ) { ... print( c.next().name); ... } Phone Service Family Plan Phone Service Core Plan Phone Service Basic Plan Phone Extended Warranty Cable TV Basic Service Package AC7 Phone AC3 Series Charger AC3 Phone AC3 Case Red AC3 Case Green AC3 Case Black
排序反了
所以答案为BC
相关文章推荐
- M102: MongoDB for DBAs chapter 2学习记录
- M102: MongoDB for DBAs chapter 1学习记录
- M102: MongoDB for DBAs chapter 2学习记录
- M102: MongoDB for DBAs chapter 1学习记录
- M102: MongoDB for DBAs chapter 2学习记录
- M102: MongoDB for DBAs chapter 1学习记录
- M102: MongoDB for DBAs chapter 2学习记录
- M102: MongoDB for DBAs chapter 1学习记录
- M102: MongoDB for DBAs chapter 2学习记录
- M102: MongoDB for DBAs chapter 1学习记录
- M102: MongoDB for DBAs chapter 2学习记录
- M102: MongoDB for DBAs chapter 1学习记录
- M102: MongoDB for DBAs chapter 2学习记录
- M102: MongoDB for DBAs chapter 1学习记录
- M102: MongoDB for DBAs chapter 2学习记录
- M102: MongoDB for DBAs chapter 1学习记录
- M102: MongoDB for DBAs chapter 2学习记录
- M102: MongoDB for DBAs chapter 1学习记录
- M102: MongoDB for DBAs chapter 2学习记录
- M102: MongoDB for DBAs chapter 1学习记录