Introduction to MongoDB

MongoDB is a NoSQL database designed for scalability, flexibility, and performance. Unlike traditional relational databases (like MySQL or PostgreSQL), MongoDB stores data in JSON-like documents called BSON (Binary JSON). This document model makes it ideal for working with dynamic or semi-structured data.
Key Concepts
Term | Description |
---|---|
Document | The basic unit of data in MongoDB. Similar to a row in a relational database, but more flexible. Written in JSON-like syntax. |
Collection | A group of MongoDB documents. Equivalent to a table in a relational database. |
Database | A container for collections. |
_id Field | A unique identifier automatically added to every document (can be custom). |
BSON | Binary representation of JSON documents, used for storage and data transfer. |
Features of MongoDB
-
Schema-less: Each document in a collection can have a different structure.
-
Scalable: Horizontal scalability via sharding.
-
High Performance: Fast read and write operations.
-
Rich Query Language: Supports powerful and flexible queries.
-
Geospatial, Full-Text Search & Aggregation Framework
Example: A MongoDB Document
{
"_id": "507f1f77bcf86cd799439011",
"name": "Unyime",
"email": "unyime@example.com",
"skills": ["JavaScript", "Python", "MongoDB"],
"profile": {
"bio": "Full-stack developer",
"active": true
}
}
Basic MongoDB Commands
Action | Command (Mongo Shell) |
---|---|
Show databases | show dbs |
Use database | use myDatabase |
Show collections | show collections |
Insert data | db.users.insertOne({ name: "Alice" }) |
Find data | db.users.find() |
Update data | db.users.updateOne({ name: "Alice" }, { $set: { name: "Alicia" } }) |
Delete data | db.users.deleteOne({ name: "Alicia" }) |
Use Cases
-
Content management systems
-
Real-time analytics
-
IoT applications
-
Mobile apps
-
E-commerce platforms
Tools & Ecosystem
-
MongoDB Atlas: Fully managed cloud database
-
Compass: GUI for interacting with MongoDB
-
Mongoose: ODM (Object Data Modeling) for MongoDB in Node.js
-
Mongo Shell: CLI for interacting with databases
Conclusion
MongoDB is a modern, document-oriented database built for today's applications that require fast, flexible, and scalable data handling. It is widely used in full-stack development, particularly with the MERN stack (MongoDB, Express, React, Node.js).