MongoDB is a commonly-used, noSQL database management system.
We’ll start by learning basic MongoDB operations. You’ll need a personal mLab account to do this.
Create a new, single-node, sandbox (free) database on mLab.com. Then, experiment with MongoDB as follows.
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).
Use a local Mongo shell to do the following.
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.
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.
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.
Now, we’ll integrate your new MongoDB database into the comments application.
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.
Install the MongoDB/Express tools.
npm install mongodb@2.2.19 --save
Note that for version compatibility, install this explicit version.
Configure your server to use your new mLab cloud database as follows.
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
).
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.
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.
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.
npm start
) or the full Webpack
development environment (npm run dev
)? Why?
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.
We will grade your work according to the following criteria: