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学习记录相关推荐

  1. M102: MongoDB for DBAs chapter 3 performance学习记录

    M102: MongoDB for DBAs chapter 3 performance学习记录 运行环境 操作系统:windows 10 家庭中文版 Mongodb :Mongodb 3.4 Mon ...

  2. M102: MongoDB for DBAs chapter 1 introduction学习记录

    M102: MongoDB for DBAs chapter 1 introduction 运行环境 操作系统:windows 10 家庭中文版 Mongodb :Mongodb 3.4 Mongod ...

  3. M102: MongoDB for DBAs chapter 4 replication学习记录

    M102: MongoDB for DBAs chapter 4 replication 运行环境 操作系统:windows 10 家庭中文版 Mongodb :Mongodb 3.4 Mongodb ...

  4. M102: MongoDB for DBAs Final Exam

    M102: MongoDB for DBAs Final Exam 运行环境 操作系统:windows 10 家庭中文版 Mongodb :Mongodb 3.4 Mongodb安装路径:E:> ...

  5. M201: MongoDB Performance chapter 1 Introduction学习记录

    M201: MongoDB Performance chapter 1 Introduction学习记录 运行环境 操作系统:windows 10 家庭中文版 Mongodb :Mongodb 3.4 ...

  6. M312: Diagnostics and Debugging chapter 4 Connectivity学习记录

    M312: Diagnostics and Debugging chapter 4 Connectivity学习记录 运行环境 操作系统:windows 10 家庭中文版 Mongodb :Mongo ...

  7. M103: Basic Cluster Administration chapter 0 Introduction学习记录

    M103: Basic Cluster Administration chapter 0 Introduction学习记录 运行环境 操作系统:windows 10 家庭中文版 Mongodb :Mo ...

  8. M312: Diagnostics and Debugging chapter 1 Introduction学习记录

    M312: Diagnostics and Debugging chapter 1 Introduction学习记录 运行环境 操作系统:windows 10 家庭中文版 Mongodb :Mongo ...

  9. M103: Basic Cluster Administration chapter 3 Sharding学习记录

    M103: Basic Cluster Administration chapter 3 Sharding学习记录 运行环境 操作系统:windows 10 家庭中文版 Mongodb :Mongod ...

最新文章

  1. Script that build Dual Stack route
  2. 购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证
  3. 编写Play 2的模块,第2部分:拦截器
  4. vc编译器编译linux平台拷贝的源码问题
  5. R第四章:基本数据管理
  6. C++11 —— 基于区间(range)的 for 循环
  7. Mac下安装emacs+cscopse+auto-complete
  8. 使用Windows 7 管理Windows 2008 R2
  9. 分享一个WIN10可用的桌面图标栅栏管理插件Fences的破解版
  10. 双重差分法之PSM - DID
  11. iOS:error: unable to read input file
  12. 数独解题算法java版
  13. 学电子信息工程,出路在哪里?
  14. 套接字的连接(服务器与客户端一对一的连接)
  15. 奔走相告!2020阿里云618年中大促火爆来袭!
  16. Imperva waf简介
  17. PyQt5 QtChart-折线图
  18. WEMOS D1 R1/R2 [ESP8266] + PCA9685 驱动舵机
  19. 2019-2020记罗振宇“时间的朋友”跨年演讲(一)
  20. 变量的基本使用Day2

热门文章

  1. 透视投影(Perspective Projection)变换推导
  2. PDF转换器pdf2cad和PDF FLY的区别比较
  3. stm32f429igt6跑linux,TouchGFX在STM32F429IGT6上的移植(FreeRTOS版本)
  4. 重要 APT攻击事件的特征枚举
  5. cocos2dX 学习笔记——音乐、音效和进度条
  6. 仓库管理系统/课程设计/ASP.NET/
  7. beyond compare比较文本时,红色总是出现在一边的规则
  8. # COCO2017 数据集下载
  9. TCP/IP分为哪几层
  10. 四叶草云演-CTF03# ereg