M001: MongoDB Basics Chapter 2: The MongoDB Query Language + Atlas 课后习题答案

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.

  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.
    M001: MongoDB Basics Chapter 2: The MongoDB Query Language + Atlas 课后习题答案_第1张图片
  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.

    提醒
    2018-08-23 15:58:34
    在 Atlas 里我们不需要再输入 group name,只需要点击 New Project 创建 project,然后创建 cluster 即可
    M001: MongoDB Basics Chapter 2: The MongoDB Query Language + Atlas 课后习题答案_第2张图片

  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.

Lab 2.1: How Many Comedies?

Problem:

  1. 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.
  2. Use Compass to connect to your sandbox cluster.
  3. 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:
M001: MongoDB Basics Chapter 2: The MongoDB Query Language + Atlas 课后习题答案_第3张图片
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:
M001: MongoDB Basics Chapter 2: The MongoDB Query Language + Atlas 课后习题答案_第4张图片
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:
M001: MongoDB Basics Chapter 2: The MongoDB Query Language + Atlas 课后习题答案_第5张图片
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:
M001: MongoDB Basics Chapter 2: The MongoDB Query Language + Atlas 课后习题答案_第6张图片
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:
M001: MongoDB Basics Chapter 2: The MongoDB Query Language + Atlas 课后习题答案_第7张图片
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:
M001: MongoDB Basics Chapter 2: The MongoDB Query Language + Atlas 课后习题答案_第8张图片
detailed answer:
As we saw in a couple examples in this chapter, $set is the only operator or modifier required for a simple update operation such as this.
参考:Update Operators


你可能感兴趣的:(MongoDB)