0% found this document useful (0 votes)
2 views

MongoDB

The document explains the structure of MongoDB, detailing how databases, collections, and documents are organized, likening them to a library, bookshelf, and book respectively. It also covers MongoDB's architecture, key components, and advantages such as flexibility, scalability, and high performance. Additionally, it highlights the use of MongoDB with Python for data operations.

Uploaded by

vy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

MongoDB

The document explains the structure of MongoDB, detailing how databases, collections, and documents are organized, likening them to a library, bookshelf, and book respectively. It also covers MongoDB's architecture, key components, and advantages such as flexibility, scalability, and high performance. Additionally, it highlights the use of MongoDB with Python for data operations.

Uploaded by

vy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

In MongoDB, the terms document, collection, and database represent

different levels of data organization. Let's break them down with an analogy:

1. Database (DB) → The Entire Library

 A database in MongoDB is like a library.

 It contains multiple collections, similar to how a library has multiple


bookshelves for different subjects.

 Example: A database named "schoolDB" can store collections for


students, teachers, and courses.

2. Collection → A Bookshelf

 A collection is like a bookshelf inside the library.

 It stores multiple documents (just like a bookshelf holds multiple


books).

 Unlike SQL tables, collections do not have a fixed schema, meaning


documents inside can have different structures.

 Example: A collection "students" inside "schoolDB" stores


documents of different students.

3. Document → A Book

 A document in MongoDB is like a book inside the bookshelf.

 It is stored in JSON-like format (BSON) and contains key-value pairs.

 Each document represents an individual record.

 Example:

 {

 "_id": 1,

 "name": "John Doe",

 "age": 20,

 "courses": ["Math", "Science"]


 }

This is a single document inside the "students" collection.

Visual Representation

Database (schoolDB)

├── Collection (students)

│ ├── Document: { "_id": 1, "name": "John", "age": 20 }

│ ├── Document: { "_id": 2, "name": "Alice", "age": 22 }

├── Collection (teachers)

│ ├── Document: { "_id": 1, "name": "Mr. Smith", "subject": "Math" }

│ ├── Document: { "_id": 2, "name": "Ms. Lee", "subject": "Science" }

Key Differences

Equivalent in
Concept Function
SQL

Databas
Database Stores multiple collections
e

Collectio
Table Stores multiple documents
n

Docume Stores actual data in JSON-like


Row (Record)
nt format

Conclusion:

 A database is a collection of collections.

 A collection is a group of documents.

 A document is an individual record stored in BSON format.


MongoDB System Architecture

MongoDB architecture consists of three major components:

1. Clients 👨‍💻

 Applications (Python, Java, Node.js, etc.) that send requests to the


database.

2. MongoDB Server

 The core database engine that handles CRUD operations,


indexing, and query execution.

 Runs on a single machine or across multiple machines for


scalability.

3. Storage Engine 💾

 Manages how data is stored on disk.

 WiredTiger is the default storage engine (supports transactions and


compression).

MongoDB Lecture Notes

1. Introduction to MongoDB

 MongoDB is a NoSQL database that stores data in a document-


oriented format (JSON/BSON).

 It is schema-less, meaning documents in the same collection can


have different fields.

 Designed for high performance, scalability, and flexibility.

 Used in big data applications, real-time analytics, and cloud-


based apps.

2. Why Use MongoDB? (Key Advantages)

✅ NoSQL & Flexible Schema – Stores data in JSON-like format, allowing


dynamic fields.
✅ Scalability – Supports horizontal scaling with sharding (splitting data
across multiple servers).
✅ High Performance – Uses indexing and in-memory storage for fast
read/write operations.
✅ Replication & High Availability – Data is automatically copied across
multiple servers (replica sets).
✅ Easy Integration – Works with Python, Java, Node.js, and more.

3. MongoDB Architecture

3.1. Key Components

 Database – A collection of multiple collections.

 Collection – A group of documents (like a table in SQL).

 Document – A record stored in BSON format (similar to JSON).

 Shard – Splits large datasets across multiple servers for scalability.

 Replica Set – Multiple copies of data for high availability.

 Index – Speeds up search queries.

3.2. MongoDB Data Model

👉 _id – Unique identifier for the document (like a primary key in SQL).
👉 Key-Value Pairs – Data is stored as key-value pairs.

4. Features of MongoDB

🔹 1. Document-Oriented Storage – Stores data in JSON/BSON format


instead of rows and columns.
🔹 2. Indexing – Uses indexes for faster searches.
🔹 3. Replication – Ensures data availability with replica sets.
🔹 4. Sharding – Distributes data across multiple machines for scalability.
🔹 5. Load Balancing – Handles large traffic efficiently.
🔹 6. Aggregation Framework – Performs complex queries and data
processing.
🔹 7. Schema-less – Different documents can have different structures.

5. Using MongoDB with Python

 Python connects to MongoDB using the pymongo library.

5.1. Installing pymongo

5.2. Connecting to MongoDB

5.3. Fetching Data


6. Basic MongoDB Commands

7. Conclusion

 MongoDB is fast, scalable, and flexible.

 It is widely used in modern web applications, big data, and real-


time analytics.

 Easily integrates with Python using pymongo for CRUD operations.

You might also like