MongoDB

Recording: MongoDB on Docker Desktop

Overview

This is an introductory lecture on MongoDB. We follow the structure of the book:

Why MongoDB/NoSQL?

Between MongoDB and PostgreSQL/MySQL/…, one will have to make a choice. For JavaScript/NodeJS, online resources and documentations have indicated that MongoDB is the popular tool of choice. It is important that we understand some of the characteristics of MongoDB that makes it attractive for certain designs. These are the followings:

  • Schema flexibility (a.k.a. “move fast, ask forgiveness”)
    • MongoDB stores data as documents (JSON/BSON): Fields can be added whenever is necessary.
    • This is advantageous for:
      • Frequent changing product requirements
      • APIs prototyping
  • Natural fit for application objects
    • NodeJS/JavaScript speaks JSON natively.
    • It is not limited by object-relational impedance mismatch.
  • Easy scaling for early-stage development.

In the long run, it is critically important to keep track of performance of your database performance.

Lesson 13: Setting up a MongoDB

Instructions for installing MongoDB directly on Windows or Linux-based environments can be found in MongoDB Documentation. However, MongoDB’s rolling versions can be problematic for production setup, as technical issues shown in previous versions of class lectures.

In our case, we will freeze all versions of NodeJS and MongoDB through containerization.

Go through Unit 3, Lesson 13 of the textbook. One important thing to keep in mind is that this book uses the local MongoDB installation, hence `dbRURL = “mongodb://localhost:27017”.

For this class, with the container structure, we are assuming a remote MongoDB server located on node webdb. You will need to change the dbURL value accordingly:

1
dbURL = "mongodb://webdb:27017",

Lesson 14: Connect a NodeJS to MongoDB

Mongoose is an Object Data Modeling (ODM) library that sits between your JavaScript/Node.js code and MongoDB.

Mongoose allows developers to define schemas for NodeJS.

Lesson 15: Further customization