Read: Class 05 Data Modeling With NoSQL Databases - 401-advanced-javascript-muna/amman-javascript-401d1 GitHub Wiki

Data Modeling With NoSQL Databases

What is NoSQL?

SQL - Structured Query Language

  • “Normal” or “Traditional” databases like Postgres, SQL Server, Oracle and the like are “Relational”.

  • in that they store data in rows and columns and link related records through a system of keys and pointers (Foreign Keys).

  • These databases are known for being highly structured.

  • You create tables by identifying the columns, their data types, and rules.

  • You Query the data using a special language (SQL - Structured Query Language) designed to bring all of that data together.

  • SQL Databases (RDS) are great for reporting or tabular data.

NoSQL

  • Based on * document storage.
  • Typically, Document data is keyed by an ID, so it acts like an Object, letting you find records in O(1) time.
  • Documents are not structure. They are pure JSON, storing and retrieving any data that you put into your JSON objects. This makes them Flexible, but unstructured, hence the name.

Document databases are great for “Read-Heavy” usage and frequent aggregations.

We will be using Mongo in this course to dive into NoSQL Systems.

NoSQL Data Modeling

  • When modeling data for a Document, it’s best to start with considering the data you NEED, not the data that’s available. You can create many different documents to shape the data you need for different purposes.
  • You do pay a price to write to multiple documents when related information changes, but the look up speed is worth it.

ORMs (Mongoose)

Using raw JSON is flexible, but problematic. Programmers don’t like surprises. Enter “Mongoose”, and “ORM” for the Mongo database.

ORM - Object Relation Mapping

Mongoose is a Node library that makes it easy for developers to not only interface with a Mongo database, but reliably structure the data (without losing flexibility). Mongoose provides 2 essential things to make this easy for you:

  1. Schema - the rules to structure your data (A Mongoose “Schema”)
  2. CRUD methods (on steroids) to work easier with the data (All return promises)

Mongo DB Clients

Running a local mongodb server ** mongod --dbpath=[/PATH/TO/DATA/FOLDER]*

Accessing Mongo Data

  • Command Line Client mongo The stock client is the easiest way to view and query your data, right from your terminal.
  • Compass Client Compass GUI Client is a slick graphical client to view your mongo data
  • IDE Plugins - Both VSCode and Webstorm have plugins that let you work with your database right in the editor.

Cloud Databases There are a few alternatives to running Mongo locally for your web servers

  • MLab - remotely hosted mongoDB systems. Easily setup a free database (or pay for more horsepower). Works great with Heroku
  • Atlas - Cloud based, highly scalable Mongo DB
  • DynamoDB - AWS NoSQL Database. Very highly scalable. Also provides a ‘mongoose’-like ORM called ‘dynamoose’
  • CosmosDB - The Microsoft Azure equivalent for Atlas and Dynamo