M001: MongoDB Basics chapter 2 The MongoDB Query Language + Atlas学习记录

运行环境

操作系统:windows 10 家庭中文版
Mongodb :Mongodb 3.4

Mongodb安装路径:E:>MongoDB\Server\3.4\bin\
Mongodb存储路径:E:>MongoDB\data

课后问题

Lab 2.0: Create an Atlas Sandbox Cluster (Ungraded)

Please note that, while we’ve labeled this as a lab, it is ungraded. This writeup is here simply to get you started on creating an Atlas cluster.

  1. Go to https://cloud.mongodb.com/links/registerForAtlas and complete the account creation form you see on that page. Please make sure you see the message “Sign up for MongoDB Atlas” at the top of the page.
  2. Once you have completed the registration form, in the next page that appears, you will be asked to choose a new group name. We use groups to manage access to Atlas clusters. Please use the name, m001-sandbox.
  3. Once you have created a group, in the next page, enter the name, Sandbox for your cluster.
  4. On the same page, select the M0 instance size. Note that the “Pricing” now changes to say “$0.00/forever”. You do NOT need to enter a credit card to create a free-tier Atlas cluster (M0). They are free.
  5. Scroll to the bottom of the cluster-creation form and enter an administrative username and password. Please enter the username, m001-student and the password, m001-mongodb-basics
  6. Once you’ve entered your username and password, click Confirm & Deploy. You will need to wait a few minutes for your cluster to be spun up.
  7. Once your cluster is ready, click on the Security tab and then on the IP Whitelist tab. Click the ADD IP ADDRESS button and, then, in the modal that pops up, click ALLOW ACCESS FROM ANYWHERE. Click the CONFIRM button and wait while the security settings for your cluster are configured.

Note that we do not generally recommend opening an Atlas cluster to allow access from anywhere. We do that for this class to minimize network issues that you might run into.

解答

搭建MongoDB Atlas(由于版本更新,与题目步骤有出入)

这是一个MongoDB 自己开发的数据库托管服务,依靠 Amazon AWS,部署和管理 MongoDB 。

按题目要求进入网址https://cloud.mongodb.com/links/registerForAtlas(有时需要翻墙才能打开),将显示注册MongoDB Atlas界面

按提示自行注册后提交,自动进入cluster创建窗口,按照题目选择免费的M0服务模式,cluster名填入Sandbox,进行下一步

创建完毕后自动进入cluster管理窗口,可看到cluster is being created字样

等待几分钟后服务创建完成,如下图所示

切换至security页面,创建新的用户, 用户名m001-student 密码 m001-mongodb-basics,权限选择admin

按题目要求创建新的ip监听,点选ALLOW ACCESS FROM ANYWHERE允许所有访问,确认

连接MongoDB Atlas

使用shell连接MongoDB Atlas:

切换回overview窗口,点选connect按钮,选择connect with the Mongo Shell

由于我本地使用的是3.4版本mongodb,选择3.4 or earlier

E:\>MongoDB\Server\3.4\bin\mongo.exe "mongodb://sandbox-shard-00-00-edzcq.mongodb.net:27017,sandbox-shard-00-01-edzcq.mongodb.net:27017,sandbox-shard-00-02-edzcq.mongodb.net:27017/test?replicaSet=Sandbox-shard-0" --ssl --authenticationDatabase admin --username m001-student --password m001-mongodb-basics
MongoDB shell version v3.4.6
connecting to: mongodb://sandbox-shard-00-00-edzcq.mongodb.net:27017,sandbox-shard-00-01-edzcq.mongodb.net:27017,sandbox-shard-00-02-edzcq.mongodb.net:27017/test?replicaSet=Sandbox-shard-0
2018-04-11T14:29:10.561+0800 I NETWORK  [thread1] Starting new replica set monitor for Sandbox-shard-0/sandbox-shard-00-00-edzcq.mongodb.net:27017,sandbox-shard-00-01-edzcq.mongodb.net:27017,sandbox-shard-00-02-edzcq.mongodb.net:27017
2018-04-11T14:29:11.977+0800 I NETWORK  [thread1] Successfully connected to sandbox-shard-00-01-edzcq.mongodb.net:27017 (1 connections now open to sandbox-shard-00-01-edzcq.mongodb.net:27017 with a 5 second timeout)
2018-04-11T14:29:12.040+0800 I NETWORK  [ReplicaSetMonitor-TaskExecutor-0] Successfully connected to sandbox-shard-00-00-edzcq.mongodb.net:27017 (1 connections now open to sandbox-shard-00-00-edzcq.mongodb.net:27017 with a 5 second timeout)
2018-04-11T14:29:13.406+0800 I NETWORK  [thread1] Successfully connected to sandbox-shard-00-02-edzcq.mongodb.net:27017 (1 connections now open to sandbox-shard-00-02-edzcq.mongodb.net:27017 with a 5 second timeout)
MongoDB server version: 3.4.14
MongoDB Enterprise Sandbox-shard-0:PRIMARY>

成功连入!

Lab 2.1: How Many Comedies?

Problem:

If you have not already loaded the video.movieDetails collection, please review the lesson "Loading Data into Your Sandbox Cluster" for a tutorial. Then, use the script, loadMovieDetailsDataset.js, provided in the handouts for the lesson, "Connecting to an Atlas Cluster from the Mongo Shell" to load the movieDetails collection.
Use Compass to connect to your sandbox cluster.
In Compass, view the video.movieDetails collection and apply the filter {genres: "Comedy"}.

How many documents in video.movieDetails match the filter {genres: “Comedy”}?

Choose the best answer:

Attempts Remaining:3 Attempts left

457
558
603
749
816

解答

按题目要求下载文件chapter_2_the_mongodb_query_language_atlas.zip,解压到E:\MongoDB\m001。此压缩包含有3个子文件夹,这次要用的是loadMovieDetailsDataset文件夹

按要求导入loadMovieDetailsDataset.js文件,在mongo shell中执行:

MongoDB Enterprise Sandbox-shard-0:PRIMARY> load("E:/MongoDB/m001/loadMovieDetailsDataset/loadMovieDetailsDataset.js")
true

配置compass连接MongoDB Atlas:

回到overview窗口,双击sandbox标签,集群服务:

可观察到服务由1主2从组成副本集模式。选择其中的主机(primary),可得到主机的host地址

开启compass,填入相关信息,并连接数据库服务

成功进入compass,执行查询:{genres: “Comedy”}

显示结果有749条数据,为所求答案

Lab 2.2: How Many Inserted?

Problem:

If the collection video.myMovies is currently empty, how many documents would be inserted by the following call to insertMany().

db.myMovies.insertMany([{"_id" : "tt0084726","title" : "Star Trek II: The Wrath of Khan","year" : 1982,"type" : "movie"},{"_id" : "tt0796366","title" : "Star Trek","year" : 2009,"type" : "movie"},{"_id" : "tt0084726","title" : "Star Trek II: The Wrath of Khan","year" : 1982,"type" : "movie"},{"_id" : "tt1408101","title" : "Star Trek Into Darkness","year" : 2013,"type" : "movie"},{"_id" : "tt0117731","title" : "Star Trek: First Contact","year" : 1996,"type" : "movie"}],{ordered: false}
);

Choose the best answer:

Attempts Remaining:3 Attempts left

1
2
3
4
5

解答

使用mongodb shell操作:
查看并切换至video库

MongoDB Enterprise Sandbox-shard-0:PRIMARY> show dbs
admin  0.000GB
local  1.121GB
video  0.001GB
MongoDB Enterprise Sandbox-shard-0:PRIMARY> use video
switched to db video
MongoDB Enterprise Sandbox-shard-0:PRIMARY> db
video

执行所给的插入数据语句:

MongoDB Enterprise Sandbox-shard-0:PRIMARY> db.myMovies.insertMany(
...   [
...     {
...       "_id" : "tt0084726",
...       "title" : "Star Trek II: The Wrath of Khan",
...       "year" : 1982,
...       "type" : "movie"
...     },
...     {
...       "_id" : "tt0796366",
...       "title" : "Star Trek",
...       "year" : 2009,
...       "type" : "movie"
...     },
...     {
...       "_id" : "tt0084726",
...       "title" : "Star Trek II: The Wrath of Khan",
...       "year" : 1982,
...       "type" : "movie"
...     },
...     {
...       "_id" : "tt1408101",
...       "title" : "Star Trek Into Darkness",
...       "year" : 2013,
...       "type" : "movie"
...     },
...     {
...       "_id" : "tt0117731",
...       "title" : "Star Trek: First Contact",
...       "year" : 1996,
...       "type" : "movie"
...     }
...   ],
...   {
...     ordered: false
...   }
... );
2018-04-11T15:07:43.606+0800 E QUERY    [thread1] BulkWriteError: write error at item 2 in bulk operation :
BulkWriteError({"writeErrors" : [{"index" : 2,"code" : 11000,"errmsg" : "E11000 duplicate key error collection: 5acd8823df9db1790b862bd7_video.myMovies index: _id_ dup key: { : \"tt0084726\" }","op" : {"_id" : "tt0084726","title" : "Star Trek II: The Wrath of Khan","year" : 1982,"type" : "movie"}}],"writeConcernErrors" : [ ],"nInserted" : 4,"nUpserted" : 0,"nMatched" : 0,"nModified" : 0,"nRemoved" : 0,"upserted" : [ ]
})
BulkWriteError@src/mongo/shell/bulk_api.js:372:48
BulkWriteResult/this.toError@src/mongo/shell/bulk_api.js:336:24
Bulk/this.execute@src/mongo/shell/bulk_api.js:1173:1
DBCollection.prototype.insertMany@src/mongo/shell/crud_api.js:302:5
@(shell):1:1

根据结果可知BulkWriteError.nInserted的值为4,成功插入了4条数据。

执行查询语句确认下:

MongoDB Enterprise Sandbox-shard-0:PRIMARY> db.myMovies.count()
4

答案为4

Lab 2.3: Queries on Scalar Fields

Problem:

Explore the movieDetails collection that you loaded into your Atlas sandbox cluster and then issue a query to answer the following question. How many movies in the movieDetails collection are rated PG and have exactly 10 award nominations?

You will find the count() method useful in answering this question using the mongo shell.

Choose the best answer:

Attempts Remaining:3 Attempts left

0
1
3
6
11

解答

根据题目要求查询movieDetails库中rated为PG,且awards.nominations为10的字段数(注意:awards.nominations的字段类型为int)

MongoDB Enterprise Sandbox-shard-0:PRIMARY> db.movieDetails.find({"rated":"PG","awards.nominations":10}).count()
3

可知答案为3

Lab 2.4: Queries on Array Fields, Part 1

Problem:

Explore the movieDetails collection that you loaded into your Atlas sandbox cluster and then issue a query to answer the following question. How many movies in the movieDetails collection list “Family” among its genres?

You will find the count() method useful in answering this question using the mongo shell.

Choose the best answer:

Attempts Remaining:3 Attempts left

20
57
124
200
277

解答

根据题目要求查询movieDetails库中genres字段包含Family的数据数,注意到genres字段存的是数组类型

MongoDB Enterprise Sandbox-shard-0:PRIMARY> db.movieDetails.find({"genres" : "Family"}).count()
124

答案为124

Lab 2.5: Queries on Array Fields, Part 2

Problem:

Explore the movieDetails collection that you loaded into your Atlas sandbox cluster and then issue a query to answer the following question. How many movies in the movieDetails collection list “Western” second among its genres?

You will find the count() method useful in answering this question using the mongo shell.

Choose the best answer:

Attempts Remaining:3 Attempts left

7
14
80
93
102

解答

根据题目要求查询movieDetails库中genres字段中第二个字段为Western的数据数,数组是从0开始计数,所以第二个的角标是1

MongoDB Enterprise Sandbox-shard-0:PRIMARY> db.movieDetails.find({"genres.1" : "Western"}).count()
14

答案为14

Lab 2.6: Update Operators

Problem:
Suppose you wish to update the value of the plot field for one document in our movieDetails collection to correct a typo. Which of the following update operators and modifiers would you need to use to do this?

Check all answers that apply:

Attempts Remaining:3 Attempts left

$set
$addToSet
$rename
$position
$inc
$unset
$slice
$mul
$push

解答

所给Operators及其作用

名字 作用
$set Sets the value of a field in a document.
$addToSet Adds elements to an array only if they do not already exist in the set.
$rename Renames a field
position|Modifiestheposition|Modifiestheposition|Modifies the push operator to specify the position in the array to add elements.
$inc Increments the value of the field by the specified amount.
$unset Removes the specified field from a document.
slice|Modifiestheslice|Modifiestheslice|Modifies the push operator to limit the size of updated arrays.
$mul Multiplies the value of the field by the specified amount.
$push Adds an item to an array.

根据题目要求,只是要替换plot字段的一个值,使用$set参数即可

参考mongodb Update Operators

M001: MongoDB Basics chapter 2 The MongoDB Query Language + Atlas学习记录相关推荐

  1. M001: MongoDB Basics Chapter 3: Deeper Dive on the MongoDB Query Language学习记录

    M001: MongoDB Basics Chapter 3: Deeper Dive on the MongoDB Query Language学习记录 运行环境 操作系统:windows 10 家 ...

  2. Kusto Query Language (KQL) 学习笔记

    Kusto Query Language (KQL) 学习笔记 Kusto 是一种nosql,其有自身的查询语言规则,简称KQL. 学习笔记持续更新中,点击每个节点的小加号,展开小节内容 KQL学习笔 ...

  3. M102: MongoDB for DBAs chapter 2 crud_and_administrative_commands学习记录

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

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

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

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

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

  6. M201: MongoDB Performance chapter 4 CRUD Operations学习记录

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

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

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

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

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

  9. M201: MongoDB Performance chapter 3 Index Operations学习记录

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

最新文章

  1. SAP HUM 没有搬到Storage Type 923的HU能用HU02拆包?
  2. leetcode 搜索插入位置
  3. Linux/windows系统定时任务调度总结
  4. 计算机新建用户会不会速度快,创建帐户让电脑系统速度比重装还快
  5. Pytorch中expand_as()函数详解
  6. mysql group 最大值_MySQL groupwise最大值为字段的长度
  7. 用python简单处理图片(5):图像直方图
  8. oracle的临时表
  9. sql server数据库导出表结构和数据
  10. 【美少女】阿里巴巴实习三个月总结
  11. Android手机SD卡分区
  12. linux快速返回家目录的命令,linux怎么使用命令返回上一级目录?
  13. 奇技淫巧之 dummy 网卡
  14. Unexpected Error: Quitting
  15. 【设计教程】photoshop自由变形工具,透视工具的使用!
  16. 【代老婆发】你是中介,你姐是黑中介,你们全家都是我爱我家的中介
  17. 从文件获取文件内容并提取promise对象的result结果
  18. 小熊派简介和环境搭建
  19. 免安装绿色软件共享 傻瓜gif制作 flash CS3 动画 影片制作
  20. Linux协议栈配置文件,Linux TCP/IP 协议栈调优 | 鸟窝

热门文章

  1. Linux系统装流媒体服务器,搭建和配置支撑2000人同时观看的流媒体服务器系统(Linux步骤详解)...
  2. 基于JavaWeb电影系统分享【源码开源】
  3. 多文件以压缩包形式下载
  4. Java GUI编程学习
  5. php在线支付之支付宝
  6. 旋变解码(RDC)芯片分析
  7. vegas pro 记录
  8. Java使用栈反转字符串
  9. Window下的2D游戏-贴图技术
  10. 对信道估计和信道均衡的理解