Last updated: May 15 16:00

MongoDB Challenge

Level 1: Getting Started

Level 1.1: Reading materials

Follow these links to learn about MongoDB.

  1. MongoDB Shell
  2. MongoDB Compass

Level 1.2 Sign up for the challenge

  1. Obtain a server from the Challenge Hub if you have not done so already.
  2. Check for an email from HACKZONE that contains information and credentials needed for you to continue.

Level 2: MongoDB CLI Example

Level 2.1: Overview

This application is going to demonstrate querying an example restaurants collection. We are going to do this using the MongoDB command line interface (CLI) within the MongoDB shell.

The exercises in this challenge use the restaurants collection in the rest database. The following is a sample document in the restaurants collection. (Source:

  "address": {
     "building": "1007",
     "coord": [ -73.856077, 40.848447 ],
     "street": "Morris Park Ave",
     "zippre": "10462"
  "borough": "Bronx",
  "cuisine"MongoDB instanceery",
  "grades": [
     { "date": { "$date": 1393804800000 }, "grade": "A", "score": 2 },
     { "date": { "$date": 1378857600000 }, "grade": "A", "score": 6 },
     { "date": { "$date": 1358985600000 }, "grade": "A", "score": 10 },
     { "date": { "$date": 1322006400000 }, "grade": "A", "score": 9 },
     { "date": { "$date": 1299715200000 }, "grade": "B", "score": 14 }
  "name": "Morris Park Bake Shop",
  "restaurant_id": "30075445"

Level 2.2 Working with MongoDB Shell

  1. Start an SSH terminal (e.g. putty on windows, or terminal in Linux or Mac) and connect to the IP address of your assigned server.
  2. Login to your assigned Linux user using the supplied password.
  3. Download the example restaurant collection.
    curl -O               
  4. Import the restaurant collection into the rest database.
    mongoimport --db rest --collection restaurants --drop -u <user> --host localhost:<port#>
                --authenticationDatabase admin --file ./primer-dataset.json  
  5. Connect to your MongoDB instance by executing the following command:
    mongo --port <port#> -u <user> -p  --authenticationDatabase admin  
    Enter password
  6. Connect to the rest database to access the restaurants collection.
    use rest              
  7. Let’s do some simple queries.

    Count the number of restaurants (documents).


    Count the number of bakeries (cuisine).

    db.restaurants.find( { cuisine: "Bakery" } ).count() 

    Display the first 5 bakeries in the collection.

    db.restaurants.find( { cuisine: "Bakery" } ).limit(5) 
  8. Insert a new restaurant (document) with the name ‘IBM Bakery’.
    db.restaurants.insert (
       "address" : {
          "building" : "123",
          "coord" : [ -73.9434351, 40.6075879 ],
          "street" : "First Street", "zip code" : "12345" },
      "grades":[ {
          "score":10 } ], 
      "name":"IBM Bakery", 
  9. Display the new count of bakeries.

    db.restaurants.find( { cuisine: "Bakery" } ).count() 
  10. Challenge yourself!

    Question: Write a query to display the new restaurant (document) by name.

Get points:: If you are participating in the hackZone challenge and would like to earn points, please return to the Advocate Hub and answer questions based on this level.

Level 3: MongoDB Compass

Level 3.1: Overview

In this section, you will use the MongoDB Compass tool that is available to download from MongoDB.  This tool will help you manage your database activity.

Level 3.2: Install MongoDB Compass

  1. If MongoDB Compass is installed already on your workstation, then go to the next step. Otherwise, follow the instructions below to install it.
    Access  this link to download MongoDB Compass for your platform. 
    Access this link for instructions to install MongoDB Compass on your local workstation. 
  2. Connect to your MongoDB instance.
    Start the MongoDB Compass application 
    In the Compass Connect tab, enter connection information specific to your MongoDB instance. 


    Click Connect

    Once connected, you should see the restaurants collection you added earlier, in the database list.


Level 3.3: Working with MongoDB Compass

  1. Let’s work with the restaurants collection.
    Click on restaurants from the left navigation bar

    You will see information and statistics about this collection.

    Question: Based on the current sample analysis which ‘borough’ contains the second largest number of restaurants?

  2. Build a query to find all the restaurants with “American” cuisine in Brooklyn.
    Click on Brooklyn from the 'borough' section of the schema.

    Image: Restaurants in Brooklyn

    Click on American from the ‘cuisine’ section of the schema.

    Image: American Cuisine in Brooklyn

    Note that the query has been generated automatically in the input bar on top of the page.

    Image: Generated query

    Run the query.

    Click APPLY

    Question: How many restaurants are returned?

    Question: What is the percentage of restaurants with a grade of ‘A’?

    Reset the query.

    Click on RESET
  3. Challenge yourself!

    Build and run a query to find the number of restaurants in the Bronx with a current grade of A.

    Question: What is the number of restaurants returned?

    Reset the query.

    Click on RESET
  4. Let’s insert a restaurant.
    Click on the DOCUMENTS tab

    Use the first document as an example to clone.

    Click on the Clone Document icon for the first document


    Update the restaurant information as you wish. For example:


    Click on INSERT

    Question: How many documents do you have in the collection now?

  5. Challenge yourself!

    Using the Map and Geo Query feature, determine the approximate number of restaurants within a half-mile radius of Grand Central Terminal in New York City, which can be seen from the corner of E42nd Street and Lexington Avenue as shown in the following example figure.

    Image: Restaurant map

Get points:: If you are participating in the hackZone challenge and would like to earn points, please return to the Advocate Hub and answer questions based on this level.