Assignment 2: MongoDB
Last updated
Last updated
Please follow MongoDB Setup to make sure you can use MongoDB locally and access MongoDB Compass (the browser for MongoDB projects).
Most of our database stack uses MongoDB for simplicity. Some more antiquated stacks (e.g. Ghost CMS) uses SQL but it's never directly exposed.
To set up, navigate to the left panel after you connect to localhost:
Click the +
button and add a database. You may call the database whatever you want. I'll name it ieee-library
for the sake of the exercise. You may also enter books
or any other collection name. This creates a collection upon database creation.
Note: In SQL, you may recognize some of these terms. "Database" refers to an overall database that contains information. However, in SQL, you may be more familiar with the word "table." A collection is similar to a table. The reason that it's different is because NoSQL (MongoDB-like) databases do not contains "rows," so it makes no sense to call something without rows and columns a "table." Instead of rows, we have "documents" that store a JSON aggregation of data.
Recall the command to open the mongo shell. Let's open it. Then we need to switch to use our new database we just created.
Then let's insert a book:
Then let's insert multiple books. You can probably see these update in MongoDB Compass after you refresh.
Now, let's list all the books:
If we wanted to find a specific entry, you can do:
If we wanted to update a document, you can do the following:
If we wanted to delete something, you can do:
If you wanted to count the number of documents:
If we wanted to filter by a field, you can do
You can sort a retrieve by
There are many more actions you can do! Please feel free to Google any of them at any time.
Since you operate a library, you want to hold information about users! Let's start thinking about how we want to store this information!
List out what information you want your user to have. Structure them in a key: value_type structure like below in JSON format. This is your "schema."
Go ahead and insert 1 new user into the database. What commands do you run?
Now it gets a bit harder! We're going to try and adapt the code from nodejs-demo
to work with your local MongoDB database!
First of all, we need a package that allows NodeJS to interface with MongoDB. Thankfully MongoDB curates their own package called MongoDB Client.
What command do you need to run to install this package? Go ahead and do so!
Let's start editing our code. Here are some lines of code that you may want to add to the project. You won't be adding this as a block somewhere. Each line is separate! They go in different places in the file. See if you can figure out where to add these.
Even after you put these in, it won't work fully. Why is that the case? What crucial step are we missing?
We won't talk about how to complete the integeration for this assignment. If you're ambitious, please go ahead and try to finish the integration! We will cover more of this in 1 or 2 weeks.