M001: MongoDB Basics Chapter 2: The MongoDB Query Language + Atlas 课后习题答案
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.
- 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.
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.
提醒
2018-08-23 15:58:34
在 Atlas 里我们不需要再输入 group name,只需要点击 New Project 创建 project,然后创建 cluster 即可
- Once you have created a group, in the next page, enter the name, Sandbox for your cluster.
- 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.
- 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.
- 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.
- 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.
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”}?
Answer:
detailed answer:
If you have loaded the movieDetails collection, the filter {genres: “Comedy”} will match 749 documents.
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 } );
Answer:
detailed answer:
There are duplicate _id values for the two entries for “Star Trek II: The Wrath of Kahn”. The second instance of this movie appearing in the array will not be inserted. However, since we are specifying that insertMany() should perform an unordered insert, all other documents will be inserts for a total of 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.
Answer:
detailed answer:
You can find this answer in either the mongo shell or in Compass.
In the shell, assuming you’ve loaded movieDetails into the video database and assuming you are connected to your Atlas sandbox cluster, you can issue the following query.
db.movieDetails.find({rated: “PG”, “awards.nominations”: 10}).count()
In Compass you can use the following filter in the Documents tab for the movieDetails collection.
{rated: “PG”, awards.nominations: 10}
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.
Answer:
detailed answer:
You can find this answer in either the mongo shell or in Compass.
In the shell, assuming you’ve loaded movieDetails into the video database and assuming you are connected to your Atlas sandbox cluster, you can issue the following query.
db.movieDetails.find({genres: “Family”}).count()
In Compass you can use the following filter in the Documents tab for the movieDetails collection.
{genres: “Family”}
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.
Answer:
detailed answer:
You can find this answer in either the mongo shell or in Compass.
In the shell, assuming you’ve loaded movieDetails into the video database and assuming you are connected to your Atlas sandbox cluster, you can issue the following query.
db.movieDetails.find({“genres.1”: “Western”}).count()
In Compass you can use the following filter in the Documents tab for the movieDetails collection.
{genres.1: “Western”}
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?
Answer:
![
s
e
t
]
(
h
t
t
p
s
:
/
/
i
m
g
−
b
l
o
g
.
c
s
d
n
.
n
e
t
/
20180823153510147
?
w
a
t
e
r
m
a
r
k
/
2
/
t
e
x
t
/
a
H
R
0
c
H
M
6
L
y
9
i
b
G
9
n
L
m
N
z
Z
G
4
u
b
m
V
0
L
3
F
x
X
z
M
0
N
T
M
w
N
j
k
2
/
f
o
n
t
/
5
a
6
L
5
L
2
T
/
f
o
n
t
s
i
z
e
/
400
/
f
i
l
l
/
I
0
J
B
Q
k
F
C
M
A
=
=
/
d
i
s
s
o
l
v
e
/
70
)
∗
∗
d
e
t
a
i
l
e
d
a
n
s
w
e
r
:
∗
∗
A
s
w
e
s
a
w
i
n
a
c
o
u
p
l
e
e
x
a
m
p
l
e
s
i
n
t
h
i
s
c
h
a
p
t
e
r
,
∗
set](https://img-blog.csdn.net/20180823153510147?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0NTMwNjk2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70) **detailed answer:** As we saw in a couple examples in this chapter, *
set](https://img−blog.csdn.net/20180823153510147?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0NTMwNjk2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)∗∗detailedanswer:∗∗Aswesawinacoupleexamplesinthischapter,∗set* is the only operator or modifier required for a simple update operation such as this.
参考:Update Operators