Duration: 6 hours (Lecture: 6 hours, Lab: 8 hours)
Syllabus Topics:
- Lecture:
- Introduction to MongoDB, Features of MongoDB
- MongoDB Command Interface and MongoDB Compass
- MongoDB Documents & Collections
- RDBMS & MongoDB Analogies: Relations/Tables => Collections; Tuples/Records => Documents
- JSON and BSON Documents
- Performing CRUD (CREATE, READ, UPDATE, DELETE) Operations, UPSERT
- MongoDB – Operators, Sorting, Indexing
- Lab (8 hours):
- Using MongoDB Shell and Compass
- Creating Database, Connecting to a Database, Creating Collections
- Performing CRUD Operations
- MongoDB: Complex Read Using Operators, Sorting Operations, Creating Indexes
Lecture Notes (In-Depth)
1. Introduction to MongoDB, Features of MongoDB
- Introduction to MongoDB:
- MongoDB is an open-source, document-oriented NoSQL database designed for scalability, flexibility, and performance.
- Uses a document model, storing data in JSON-like BSON (Binary JSON) documents within collections.
- Ideal for applications requiring dynamic schemas, large-scale data, and high availability (e.g., e-commerce, social media, IoT).
- Features of MongoDB:
- Document-Based: Stores data as documents (BSON), allowing nested structures and varied fields.
- Example:
{ "name": "Alice", "skills": ["Java", "Python"] }
.
- Schema-Less: No fixed schema; documents in a collection can have different structures.
- Horizontal Scaling: Supports sharding (data partitioning across servers) and replication (data copies for redundancy).
- High Availability: Replica sets ensure failover and data redundancy.
- Rich Query Language: Supports complex queries, aggregations, and indexing.
- Indexing: Supports various index types (e.g., single, compound, text) for performance.
- Aggregation Framework: Pipeline-based processing for data transformation (e.g., group, filter, sort).
- Ad Hoc Queries: Real-time querying without predefined schemas.
- Geospatial Queries: Supports location-based queries (e.g., find nearby users).
- CAP Theorem: MongoDB is CP (Consistency, Partition Tolerance) by default but can be tuned for AP (Availability, Partition Tolerance).
- Key Notes:
- MongoDB is widely used in web applications, big data, and real-time analytics.
- Comparison to MySQL: MongoDB sacrifices strict ACID compliance for scalability and flexibility.
2. MongoDB Command Interface and MongoDB Compass
- MongoDB Command Interface (mongosh):
mongosh
is the MongoDB Shell, a JavaScript-based CLI for interacting with MongoDB.
- Commands:
- Connect:
mongosh "mongodb://localhost:27017"
.
- Show databases:
show dbs
.
- Switch database:
use mydatabase
.
- Show collections:
show collections
.
- Used for executing CRUD operations, managing indexes, and running administrative tasks.
- MongoDB Compass:
- A GUI tool for MongoDB, providing a visual interface to explore, query, and manage data.
- Features:
- Browse databases and collections.
- Create and execute queries with a visual query builder.
- Analyze schema and performance (e.g., index usage).
- Manage indexes and run aggregations.
- Example: Use Compass to insert a document or view query execution plans.
- Key Notes:
mongosh
is ideal for scripting and automation; Compass is better for visual exploration.
- Both tools support authentication and connect to local or cloud-hosted MongoDB instances.
3. MongoDB Documents & Collections
- Documents:
-
Basic unit of data in MongoDB, stored as BSON (JSON-like key-value pairs).
-
Example:
{
"_id": ObjectId("507f1f77bcf86cd799439011"),
"name": "Alice",
"salary": 60000,
"dept": "IT"
}
-
_id
: A unique identifier (auto-generated as ObjectId
unless specified).
- Collections:
- Groups of documents, analogous to tables in RDBMS.
- No predefined schema; documents in a collection can have different fields.
- Example: A
employees
collection may contain documents with varying attributes.
- Key Notes:
- Collections are created implicitly when inserting the first document.
- Documents are flexible but should follow a logical structure for application consistency.
4. RDBMS & MongoDB Analogies
RDBMS |
MongoDB |
Database |
Database |
Table/Relation |
Collection |
Row/Tuple/Record |
Document |
Column |
Field |
Primary Key |
_id Field |
Join |
$lookup (Aggregation) |
SQL Query |
MongoDB Query Language |