MongoDB is a commonly-used, noSQL database management system.

Using MongoDB

We’ll start by learning basic MongoDB operations. You’ll need a personal mLab account to do this.

Exercise 10.1

Create a new, single-node, sandbox (free) database on mLab.com. Then, experiment with MongoDB as follows.

  1. Use the mLab web interface to create your own cs336 database. Add a cs336 user with write access. Use the class password for this user (so that the database can be accessed and graded).

  2. Use a local Mongo shell to do the following.

    1. Create a collection for your new cs336 database. You can find the command for accessing this cloud database on the mLab homepage for your new database.
    2. Populate your new collection with documents for the first two comments from the ReactJS tutorial (from Hunt and O’Shannessy).

    Recall that in lab 8, you started by hard-coding these comments in JavaScript on the server and then moved them to a separate file (comments.json). Here, you’re moving them once again, this time to a cloud database.

  3. Make sure that you can perform all the basic CRUD operations on documents in your new database. Add a new comment document, edit it and then delete it. You should be able to do this using both the Mongo command-line interface (for documentation, see MongoDB CRUD Operations) as you’ve done so far and using the mLab web interface.

You’ll integrate this database into a RESTful API below. Take time now to consider the following questions.

  1. Compare and contrast MongoDB and the SQL databases you’ve used before. Are there things that one can do that the other can’t?
  2. You could run MongoDB locally. What advantages and/or disadvantages would that bring with respect to the cloud database you have now?
  3. You’ve built a “sandbox” database on mLab. Could you use this for a production database? Why or why not?

Save your answers to these questions in a lab10.txt file in the root of your lab repo.

These exercises could be done on the local MongoDB installation as well.

Integrating MongoDB into the Course Application

Now, we’ll integrate your new MongoDB database into the comments application.

Exercise 10.2

Make a copy of your solution for the previous lab and store it under lab10. Be sure to copy all the files; the Linux cp command will not copy the .babelrc by default. Now, integrate your new MongoDB database into your existing comments application as follows.

  1. Install the MongoDB/Express tools.

    npm install mongodb@2.2.19 --save

    Note that for version compatibility, install this explicit version.

  2. Configure your server to use your new mLab cloud database as follows.

    1. Load the MongoDB library and initialize a connection to your new database as shown in this example: Express: MongoDB. Notes:

      • Use the code version that matches mLab’s MongoDB version (v3.6.6).

      • Set a UNIX environment variable holding the MongoDB password.

        export MONGO_PASSWORD=course-password
      • Replace the local MongoDB connection string in their example with the connection string for your mLab-based Mongo DB. You can find this on the mLab homepage for your database. Splice the following expression into this connection string to access the MongoDB password you set above.

        process.env.MONGO_PASSWORD

        This approach allows you to avoid hard-coding your password in your server file (and pushing it to GitHub).

      • Remove the (local) DB retrieval code in the sample connect() call-back method and replace it with a simple assignment that saves the db handle for use in your server’s route handers.

        db = client;

        Declare this variable globally in the server module so that all the route handler methods can use it. Here, client should be the second parameter to the call-back function; make sure that this parameter name doesn’t collide with any global variable names you’re already using (e.g., db).

      • Move the call to app.listen() into the body of the call-back method for MongoClient.connect(), after doing the db connection assignment. This ensures that the database connection is made before starting the server.
    2. Remove the file-handling code from your server and replace the data-access code in the route handlers (i.e., GET and POST on /api/comments) with equivalent database access code. Notes:

      • You can find the NodeJS/MongoDB API CRUD operations introduced here: Quick Start (for more details, see the CRUD Operations Tutorial).

      • You can continue to use the current date (Date.now()) as the ID.

      • Get rid of comments.json; you’re reading data from mLab/MongoDB now.

  3. Re-build and re-run the build and development server configurations. Make sure that you can still add new comments using the comment form and check that they are displayed by the client app and that they are stored on the mLab database. You can delete comments manually on the mLab website.

When everything is running again, consider the following questions.

  1. For this integration of mLab into your application, was it better to run the modified server (npm start) or the full Webpack development environment (npm run dev)? Why?
  2. Explain what happens when you modify the database using mLab’s web interface rather than your application? Try adding/updating/deleting comment documents.
  3. Is this comments application the sort of application that should use an SQL or a noSQL database? Explain your answer.

Save your answers to these questions in your lab10.txt file.

Be sure to leave your mLab database and user active so that it can be graded.

You can compare your solution with the one in the course repo.

Checking in

We will grade your work according to the following criteria: