操作系统:windows 10 家庭中文版
Mongodb :Mongodb 3.4
Mongodb安装路径:E:>MongoDB\Server\3.4\bin\
Mongodb存储路径:E:>MongoDB\data
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.
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 自己开发的数据库托管服务,依靠 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允许所有访问,确认
使用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>
成功连入!
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条数据,为所求答案
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
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
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
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
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|Modifiesthe p o s i t i o n | M o d i f i e s t h e 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|Modifiesthe s l i c e | M o d i f i e s t h e 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