Lesson 2 of 50 – Introduction to NoSQL Database
4%

Introduction to NoSQL Database

A NoSQL database is a type of database designed to store and manage data using models other than the traditional relational table model. NoSQL databases are useful for applications that need flexible data structures, scalability, or specialized data access patterns.

1. What Is NoSQL?

NoSQL commonly means "Not Only SQL." These databases can store data in documents, key-value pairs, wide-column structures, or graph structures.

MongoDB is a document-oriented NoSQL database. It stores information in documents rather than requiring every record to follow the same table structure.

Example of a MongoDB document:

{
    "name": "Rahul",
    "age": 20,
    "course": "ADCA"
}

This document contains three fields: name, age, and course.

2. Why Do We Need NoSQL Databases?

Modern applications may manage large amounts of structured, semi-structured, or rapidly changing data. NoSQL databases provide different storage models for these requirements.

  • Flexible data structure: Documents can contain different fields.
  • Scalability: Some NoSQL systems support distributing data across servers.
  • Application development: Document structures can closely match objects used in application code.
  • Specialized data access: Different NoSQL models suit different types of workloads.
  • Large datasets: Suitable NoSQL systems can support applications with substantial data and traffic.
NoSQL is not automatically better than SQL. The best choice depends on the application's data, queries, consistency requirements, and workload.

3. Types of NoSQL Databases

1. Document Database

Stores data as documents containing field-value pairs.

Example: MongoDB

{
    "student_id": 101,
    "name": "Amit",
    "course": "Web Development"
}

2. Key-Value Database

Stores data as a key associated with a value.

Example: Redis

student_name: "Amit"
student_course: "ADCA"

3. Wide-Column Database

Organizes data using rows and flexible groups of columns, making it useful for certain distributed workloads.

Example: Apache Cassandra


4. Graph Database

Stores data as nodes and relationships. It is useful when connections between entities are important.

Example: Neo4j

4. SQL vs NoSQL

SQL Database NoSQL Database
Commonly stores data in tables May use documents, key-value pairs, graphs, or wide columns
Usually uses a defined relational schema Schema flexibility depends on the database model
Uses SQL for relational database operations Uses database-specific query methods or languages
Relationships often use foreign keys and joins Relationships may use references, embedded data, or graph connections
Example: MySQL Example: MongoDB

5. Structured, Semi-Structured, and Unstructured Data

Structured Data

Data organized according to a defined structure, such as rows and columns in a relational table.

StudentID | Name  | Age
101       | Rahul | 20
102       | Amit  | 21
Semi-Structured Data

Data that uses labels, keys, or other organizational elements without requiring every record to have an identical structure.

{
    "name": "Rahul",
    "age": 20
}

{
    "name": "Amit",
    "course": "ADCA"
}
Unstructured Data

Data that does not naturally fit a fixed tabular structure, such as images, audio, video, and free-form documents. Applications may store these files separately and keep their metadata in a database.

6. Advantages of NoSQL Databases

  • Can support flexible data models.
  • Can scale horizontally when the system supports it.
  • Offers multiple storage models for different needs.
  • Can be useful for rapidly changing application data.
  • Some models simplify access to nested or related data.

7. Disadvantages and Limitations

  • Query languages and features can differ between NoSQL products.
  • Flexible schemas require careful validation and consistent application design.
  • Some data relationships may be harder to manage without relational constraints.
  • Distributed systems can introduce additional operational complexity.
  • A poorly designed data model can lead to inefficient queries or duplicated data.

8. Practical MongoDB Example

We can create a database and insert a document using MongoDB Shell (mongosh).

Step 1: Select a database

use school_db

Step 2: Insert a student document

db.students.insertOne({
    name: "Rahul",
    age: 20,
    course: "ADCA"
})

Step 3: Retrieve student documents

db.students.find()

The students collection stores the document. MongoDB creates the collection automatically when needed during this insert operation.

9. Practice Assignment

  1. What is a NoSQL database?
  2. What does NoSQL commonly stand for?
  3. Name the four main types of NoSQL databases.
  4. Write two differences between SQL and NoSQL databases.
  5. Give one example of a document database.
  6. Explain structured and semi-structured data.
  7. Write a MongoDB document for an employee.
  8. Write a command to insert a document into an employees collection.

🧠 Quick Quiz

Q1. What does NoSQL commonly mean?




Q2. Which of the following is a document database?




Q3. Which database model stores nodes and relationships?