Mongo 101

February 25, 2019

What is MongoDB

MongoDB is a NoSQL Database, also commonly known as a Non-relational (hence NoSQL) database. MongoDB functions based on collections and documents and exposes a Javascript API that we can use to interact with the database.

MongoDB does not support schemas in contrast to its RDBMS alternatives. Data is instead stored in a format very similar to JSON (Javascript Object Notation) where fields can be added dynamically.

MongoDB vs RDBMS

RDBMS MongoDB
Database Database
Table Collection
Row Document
Field Key:Value Pairs

Example Data in PostgreSQL vs MongoDB

PostgreSQL Data Representation

An example table of student's, their courses, and their grades

MongoDB Data Representation

A Student collection which contains two documents of students and their course information

{
  "_id": ObjectId("507f1f77bcf86cd799439011"),
  "studentId": 100,
  "firstName": "Jonathan",
  "middleName": "Eli",
  "lastName": "Tobin",
  "classes": [
    {
      "courseId": "PHY101",
      "grade": "B",
      "courseName": "Physics 101",
      "credits": 3
    },
    {
      "courseId": "BUS101",
      "grade": "B+",
      "courseName": "Business 101",
      "credits": 3
    }
  ]
},
{
  "_id": ObjectId("507f191e810c19729de860ea"),
  "studentId": 101,
  "firstName": "Meathead",
  "middleName": "Rob",
  "lastName": "Lowe",
  "classes": [
    {
      "courseId": "PHY101",
      "grade": "F",
      "courseName": "Physics 101",
      "credits": 3
    }
  ]
}

MongoDB Database Design and Modelling

MongoDB schemas can be designed in two ways. Since Mongo does not utilize tables to organize data, and does not support joins, we utilize embedding and referencing to associate documents with other collections.

Embedding

Embedding is the process where common data that is referenced often is moved into the parent document to reduce additional queries.

In the example above, we could have made the “classes” field into its own classes collection and queried for both students and all of their classes. Instead of requiring an additional query, we structured our student object to contain the classes associated to the student.

Embedding Data:

Operations on Embedded Data::

Referencing

Referencing is similar to its RDBMS counterparts where common data can be stored in one location and be linked from multiple different locations.

If we take the student classes example from above and refactor the data model to use reference instead,here is what we would get. Pay attention to the “classes” ObjectId and the “_id” of the class that has been referenced.

Student Document

{
  "_id": ObjectId("507f191e810c19729de860ea"),
  "studentId": 101,
  "firstName": "Meathead",
  "middleName": "Rob",
  "lastName": "Lowe",
  "classes": [
    ObjectId("54759eb3c090d83494e2d804")
  ]
}


Classes Document

{
  "_id": ObjectId("54759eb3c090d83494e2d804"),
  "studentId": ObjectId("507f191e810c19729de860ea"),
  "courseId": "PHY101",
  "grade": "F",
  "courseName": "Physics 101",
  "credits": 3
}

Referencing Data:

Operations on Referenced Data::

Embedding vs Referencing

Embedding Referencing
Better for data that will be accessed often Better for data that will not be accessed often
Use for data that does not change frequently Use for data that is required to be consistent across multiple documents and may be used in many places
Querying and inserting data is efficient Updating and inserting data is very efficient
Write operations are atomic at the document level  

Common Design Guidelines

MongoDB Basic Queries

Creating Collections:

Dropping Collections:

Querying Data:

Inserting Data:

Updating Data:

Deleting Data:

Other:

Further Reading