Learnwizy Technologies Logo

Learnwizy Technologies

Class 30: NoSQL Databases - MongoDB Basics

In the previous classes, we focused on relational databases and SQL. While powerful and widely used, relational databases might not always be the best fit for every type of data or application need, especially when dealing with large volumes of unstructured or semi-structured data, or requiring extreme horizontal scalability.

Today, we introduce NoSQL databases, a diverse category of databases that provide alternatives to the traditional relational model. We'll specifically focus on MongoDB, a popular document-oriented NoSQL database.


Introduction to NoSQL Databases


Categories of NoSQL Databases

NoSQL databases come in various types, each optimized for different data models:


Focus on MongoDB (Document Database)

MongoDB is a leading open-source, document-oriented NoSQL database. It stores data in flexible, JSON-like documents, meaning fields can vary from document to document and data structure can be changed over time.


MongoDB Core Concepts


Setting Up MongoDB Locally

You can install MongoDB Community Server on your machine or use Docker, which is often preferred for development.

Using MongoDB Compass (GUI):

MongoDB Compass is a free, official GUI for MongoDB. It allows you to visually explore your data, run ad-hoc queries, and interact with your database. Download it from MongoDB website.


Basic MongoDB Operations (CLI/Compass)

You can interact with MongoDB using the mongosh shell (CLI) or MongoDB Compass. Here are some basic operations:

1. Creating Databases and Collections:

In MongoDB, databases and collections are created implicitly when you first store data in them.

// In mongosh or Compass "Shell" tab
use mybookstore; // Switches to or creates 'mybookstore' database
db.books.insertOne({ title: "Moby Dick", author: "Herman Melville" }); // Creates 'books' collection and inserts a document

2. Inserting Documents:

3. Finding Documents:

4. Updating Documents:

5. Deleting Documents:


Advantages and Disadvantages of NoSQL vs. SQL

Feature SQL (Relational) NoSQL (Non-Relational)
Schema Strict, predefined schema. Changes require migrations. Flexible, dynamic schema (schema-less). Easy to evolve.
Data Model Tables with rows and columns. Relationships via foreign keys. Various: Document, Key-Value, Column-Family, Graph.
Scalability Primarily vertical scaling (more powerful server). Horizontal scaling (sharding) is complex. Primarily horizontal scaling (distribute across many servers).
ACID Compliance Strong ACID (Atomicity, Consistency, Isolation, Durability) guarantees for transactions. Often BASE (Basically Available, Soft state, Eventual consistency). Transactions are more complex or limited.
Query Language SQL (Structured Query Language) - powerful for complex joins. Object-oriented query languages, APIs. No standard language.
Use Cases Complex transactions, strong data integrity, structured data (e.g., financial systems, traditional ERP). Large volumes of unstructured/semi-structured data, rapid development, real-time apps, flexible data models (e.g., social media, IoT, content management).

When to choose which type of database:

Many modern applications use a combination of both SQL and NoSQL databases (polyglot persistence), choosing the best tool for each specific data storage need.

In the next class, we'll connect our Node.js/Express API to MongoDB using Mongoose, an ODM (Object Data Modeling) library, and perform CRUD operations.