MongoDB

Projected Time

About 4 - 5 hours

Prerequisites

Motivation

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?

Objectives

Participants will be able to:

Materials

Lesson

Common Mistakes / Misconceptions

Guided Practice

Techtonica staff will assign pairs.

Activity 1: Installation

  1. Check to see if you have Homebrew installed on your laptop. From your Terminal, run the brew help command. If you have Homebrew installed, you’ll see the output from Homebrew appear in your Terminal.
  2. If no Homebrew-related text appears, you’ll need to install Homebrew. Go to the Homebrew website and follow the installation instructions. Ask for help if needed.
  3. Mongo DB is no longer in the main Homebrew repo. You need to register an add-on using brew tap mongodb/brew
  4. Install MongoDB using brew install mongodb-community@4.2 in the Terminal
  5. Start it up as a service with brew services start mongodb-community@4.2
  6. From a terminal run mongo to connect
  7. If there is an error, troubleshoot using the Mongo Community on Mac docs

Activity 2: Working with Database

Independent Practice

  1. Read through these MongoDB docs from TutorialsPoint. You don’t have to memorize it, but think about how MongoDB compares to SQL as you read.
  1. Go through the following tutorial and follow the steps to build a basic API:

Challenge

$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.

Check for Understanding

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.

A Note About Mongoose

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.

Supplemental Materials