About 4 - 5 hours
Not all data can be or should be stored in a relational way. In those cases, it is more efficient to use a NoSQL database. MongoDB is one of the most popular NoSQL databases. MongoDB stores data in JSON-like “documents,” which is familiar to folks who use JavaScript.
Which companies use MongoDB?
Participants will be able to:
Techtonica staff will assign pairs.
Activity 1: Installation
brew help
command. If you have Homebrew installed, you’ll see the output from Homebrew appear in your Terminal.brew tap mongodb/brew
brew install mongodb-community@4.2
in the Terminalbrew services start mongodb-community@4.2
mongo
to connectActivity 2: Working with Database
The command use <db>
sets the current database you’ll be working with. In the shell, run use techtonica
. Then run show dbs
, which will list out the list of databases. What database is there? What is missing?
The newly created database, “techtonica”, should be missing. This is because the techtonica
database is empty. Let’s insert some data storing people’s name (as a string) and birth month (as an integer). Insert a document by running db.classmates.insert({"name": "your_name", "month": your_birthmonth})
. Do it at least two more times with a friend’s name. Remember not to double-quote the birth month to keep it as a digit instead of a string.
Type show dbs
again and see that the techtonica
database now shows up.
MongoDB stores documents in collections. Run show collections
. What is already there? Where did it come from?
Run db.createCollection("volunteers")
.
View what’s in each collection by running db.classmates.find()
and db.volunteers.find()
. What’s the difference?
Fix the empty volunteer collection by entering a document: db.volunteers.insert({"volunteer": "Adrien"})
and db.volunteers.insert({"volunteer": "Jamie"})
.
Now we’ll try out the remove
command. Select a number between your birth month and your pair’s birth month inside the classmates collection. Run db.classmates.remove({"month": {$lt: the_number_your_selected}})
. The “$lt” means “less than.” What do you think happened to the collections?
Run db.classmates.find()
. Were you correct?
Run db.volunteers.remove({})
, then db.volunteers.find()
. What happened?
Make sure you are in the techtonica database by running use techtonica
. Make sure the output is switched to db techtonica
. Then, you will delete the techtonica database by running db.dropDatabase()
. The output should be { "dropped" : "techtonica", "ok" : 1 }
.
Exit the shell by running exit
. Next, go to the Terminal window running the Mongo Daemon, monogod. Exit the daemon by entering Ctrl C.
Part of the tutorial requires a mLab account. You can create one by following these directions.
Optionally, you can reference this video which uses the same technologies to build an API.
“$lt" was used earlier to filter out which documents you deleted. MongoDB's documentation has a page on [Operators](https://docs.mongodb.com/manual/reference/operator/query/). What type of operator is "$lt”?
Operators can be used to filter what you want to include and what you want to exclude. For example, db.classmated.find({"month": {$lt: 6}})
will output anyone born before July (if you set your_birthmonth “July” as 6; because January is 0 in DateTime). You will be testing some of it next.
Open up the MongoDB daemon and shell again, create a database named “filterData”. Create a collection named “zoo”. Create at least 3 documents in the following format: {"type": "lion", "name": "Suzy", "age": 10}
. Look at MongoDB’s Operator page and find at least one operator other than Comparison Operator (which $lt was), and then test it out in the zoo collection.
List out the steps to store data in MongoDB. Find a classmate. One of you will try to explain the steps by comparing it to organizing books, and the other will compare it to organizing kitchen utensils.
Further practice with Mongoose (one ORM for Mongo) is highly encouraged if you plan on creating a project that includes a Mongo database. If that’s the case, be sure to check out the Mongoose materials below.