M102: MongoDB for DBAs chapter 2 crud_and_administrative_commands学习记录
M102: MongoDB for DBAs chapter 2 crud_and_administrative_commands
运行环境
操作系统:windows 10 家庭中文版
Mongodb :Mongodb 3.4
Mongodb安装路径:E:>MongoDB\Server\3.4\bin\
Mongodb存储路径:E:>MongoDB\data
课后习题
2.1题目
Download Handouts:
Homework2__hw2.1_m102_529fe537e2d42347509fb412.js
Products__hw1.2_m102_529e39a8e2d42347509fb3f0.json
We will use the pcat.products collection from week 1. So start with that; if not already set up, import it:
mongoimport --drop -d pcat -c products products.json
You can find products.json from the Download Handouts link.
In the shell, go to the pcat database. If you type:
use pcat;
db.products.count()
the shell should return 11.
Next, download homework2.js from the Download Handouts link. Run the shell with this script:
mongo --shell pcat homework2.js
First, make a mini-backup of the collection before we start modifying it. In the shell:
b = db.products_bak; db.products.find().forEach( function(o){ b.insert(o) } )// check it worked:
b.count()
// should print 11
If you have any issues you can restore from “products_bak”; or, you can re-import with mongoimport. (You would perhaps need in that situation to empty the collection first or drop it; see the –drop option on mongoimport –help.)
In the shell, type:
homework.a()
What is the output? (The above will check that products_bak is populated.)
Enter answer here:
3.05
解答
启动我的mongod:
C:\Users\Shinelon>e:
E:\>MongoDB\Server\3.4\bin\mongod.exe --dbpath \MongoDB\data
按题目要求导入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
2018-03-29T11:08:46.497+0800 connected to: localhost
2018-03-29T11:08:46.552+0800 dropping: pcat.products
2018-03-29T11:08:46.640+0800 imported 11 documents
进入mongo验证导入是否正确:
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.count()
11
> exit
bye
再按题目要求导入Homework2__hw2.1_m102_529fe537e2d42347509fb412.js脚本
E:\>MongoDB\Server\3.4\bin\mongo.exe --shell pcat C:\Users\Shinelon\Downloads\Homework2__hw2.1_m102_529fe537e2d42347509fb412.js
MongoDB shell version v3.4.6
connecting to: mongodb://127.0.0.1:27017/pcat
MongoDB server version: 3.4.6
type "help" for help
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]
验证是否成功:
> b = db.products_bak;
pcat.products_bak
> b.count()
11
求出第一题的答案:
> homework.a()
3.05
2.2题目
Add a new product to the products collection of this form:
{"_id" : "ac9","name" : "AC9 Phone","brand" : "ACME","type" : "phone","price" : 333,"warranty_years" : 0.25,"available" : true
}
Note: in general because of the automatic line continuation in the shell, you can cut/paste in the above and shouldn’t have to type it all out. Just enclose it in the proper statement(s) to get it added.
Next, load into a shell variable the object corresponding to
_id : ObjectId("507d95d5719dbef170f15c00")
- Then change term_years to 3 for that document. (And update it in the database.)
- Then change over_rate for sms in limits to 0.01 from 0. Update that too.
At the shell prompt type:
homework.b()
What is the output?
Enter answer here:
0.050.019031
解答
查看整个文档数据情况:
> 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 } }, "sales_tax" : true, "term_years" : 2 }
{ "_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" }
按题目要求插入数据:
> abc={
... "_id" : "ac9",
... "name" : "AC9 Phone",
... "brand" : "ACME",
... "type" : "phone",
... "price" : 333,
... "warranty_years" : 0.25,
... "available" : true
... }
{"_id" : "ac9","name" : "AC9 Phone","brand" : "ACME","type" : "phone","price" : 333,"warranty_years" : 0.25,"available" : true
}
> db.products.insert(abc)
WriteResult({ "nInserted" : 1 })
验证:
> 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 } }, "sales_tax" : true, "term_years" : 2 }
{ "_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" }
{ "_id" : "ac9", "name" : "AC9 Phone", "brand" : "ACME", "type" : "phone", "price" : 333, "warranty_years" : 0.25, "available" : true }
最后一条就是新插入的那条数据
查看题目中需要修改的文档情况
> db.products.find({_id : ObjectId("507d95d5719dbef170f15c00")})
{ "_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 } }, "sales_tax" : true, "term_years" : 2 }
首先修改term_years为3
> db.products.update({_id : ObjectId("507d95d5719dbef170f15c00")}, {"$set" : {"term_years" : 3}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
验证:
> db.products.find({_id : ObjectId("507d95d5719dbef170f15c00")})
{ "_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 } }, "sales_tax" : true, "term_years" : 3 }
再修改 limits中的 sms 中的over_rate (这里有3层嵌套)
> db.products.update({_id : ObjectId("507d95d5719dbef170f15c00")}, {"$set" : {"limits.sms.over_rate" : 0.01}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
验证方法同上
求出结果:
> homework.b()
0.050.019031
2.3题目
How many products have a voice limit? (That is, have a voice field present in the limits subdocument.)
Input your answer below, (just a number, no other characters).
While you can parse this one by eye, please try to use a query that will do the work of counting it for you.
Just to clarify, the answer should be a number, not a document. There should be no brackets, spaces, quotation marks, etc.
Enter answer here:
3
解答
有2种方式:
第一种用exists:
> db.products.count({"limits.voice" : {"$exists" : true}})
3
第二种用null
> db.products.count({"limits.voice" : {$ne:null}})
3
需要注意的是exists无法使用索引,所以推荐第二种
M102: MongoDB for DBAs chapter 2 crud_and_administrative_commands学习记录相关推荐
- M102: MongoDB for DBAs chapter 3 performance学习记录
M102: MongoDB for DBAs chapter 3 performance学习记录 运行环境 操作系统:windows 10 家庭中文版 Mongodb :Mongodb 3.4 Mon ...
- M102: MongoDB for DBAs chapter 1 introduction学习记录
M102: MongoDB for DBAs chapter 1 introduction 运行环境 操作系统:windows 10 家庭中文版 Mongodb :Mongodb 3.4 Mongod ...
- M102: MongoDB for DBAs chapter 4 replication学习记录
M102: MongoDB for DBAs chapter 4 replication 运行环境 操作系统:windows 10 家庭中文版 Mongodb :Mongodb 3.4 Mongodb ...
- M102: MongoDB for DBAs Final Exam
M102: MongoDB for DBAs Final Exam 运行环境 操作系统:windows 10 家庭中文版 Mongodb :Mongodb 3.4 Mongodb安装路径:E:> ...
- M201: MongoDB Performance chapter 1 Introduction学习记录
M201: MongoDB Performance chapter 1 Introduction学习记录 运行环境 操作系统:windows 10 家庭中文版 Mongodb :Mongodb 3.4 ...
- M312: Diagnostics and Debugging chapter 4 Connectivity学习记录
M312: Diagnostics and Debugging chapter 4 Connectivity学习记录 运行环境 操作系统:windows 10 家庭中文版 Mongodb :Mongo ...
- M103: Basic Cluster Administration chapter 0 Introduction学习记录
M103: Basic Cluster Administration chapter 0 Introduction学习记录 运行环境 操作系统:windows 10 家庭中文版 Mongodb :Mo ...
- M312: Diagnostics and Debugging chapter 1 Introduction学习记录
M312: Diagnostics and Debugging chapter 1 Introduction学习记录 运行环境 操作系统:windows 10 家庭中文版 Mongodb :Mongo ...
- M103: Basic Cluster Administration chapter 3 Sharding学习记录
M103: Basic Cluster Administration chapter 3 Sharding学习记录 运行环境 操作系统:windows 10 家庭中文版 Mongodb :Mongod ...
最新文章
- MSSQL2005外网IP的1433端口开启方法
- java读取指定package下的所有class
- mingw w64 matlab,Matlab安装MinGW-w64问题解决
- 软件文档 服务器上,服务器上需要什么软件
- centos mysql自动备份_CentOS下每天自动备份mysql数据库
- php slaveokay 设置,PHP: MongoCursor::slaveOkay - Manual
- 6.1(数学:五角数)
- 甘肃暴雨强度公式_最新给排水计算软件,16大功能常用公式自动计算,配11套施工方案...
- java中如何实现货币兑换_java货币转换
- python开发单片机仿真软件_开源电子电路仿真模拟软件,加油国产芯片
- RGBA(0,0,0,0)调色
- python 之hellow
- 如何重新设置苹果id密码_苹果手机ID密码忘了?别着急,这二种方法轻松帮你搞定!...
- APS系统哪家好(下)
- Learn How Google Works: in Gory Detail
- redis的多路复用原理
- 关于直播的iOS开发
- 剪贴板操作 Clipboard API 使用教程
- 4.18、TCP滑动窗口
- 蓝桥杯java历年真题及答案整理(共100道题目及答案)