🍃 MongoDB Tutorial
← PrevNext →
MongoDB Tutorial: Introduction to MongoDB (NoSQL Database)
Learn what MongoDB is, how it stores data as documents, and how it compares to a traditional RDBMS like MySQL — with a real BSON example.
Introduction to MongoDB
MongoDB is an open-source, document-oriented database and the most widely used NoSQL database in the world. Unlike a traditional relational database, MongoDB doesn't store data in rows and tables — it stores data as flexible, JSON-like documents.
- MongoDB is written in C++.
- It can be installed locally or used as a fully managed cloud service (MongoDB Atlas).
- It stores data in BSON (Binary JSON) — a binary-encoded format that extends JSON with extra types like dates and binary data.
Example Document
{
"_id": { "$oid": "644988f4d7d9678758f1fc21" },
"name": "Rahul Gupta",
"roll": "1",
"address": "nsp",
"marks": [550]
}
MongoDB vs RDBMS
MongoDB works on the concept of collections and documents, which map to familiar relational concepts:
| RDBMS | MongoDB |
|---|---|
| Database | Database |
| Table | Collection |
| Row | Document |
| Column | Field |
| Table Join | Embedded Document |
Database
- A database is a physical container for collections.
- Each database gets its own set of files on the file system.
- A single MongoDB server typically hosts multiple databases.
Advertisement728 x 90
Collection
- A collection is a group of MongoDB documents — the equivalent of a table in an RDBMS.
- A collection exists within a single database.
- Collections do not enforce a schema — documents in the same collection can have completely different fields.
Document
- A document is a set of key-value pairs, similar to a JSON object.
- Documents have a dynamic schema — fields and data types can vary between documents in the same collection.
- Every document has a unique
_idfield:- By default it's a 24-character hex string (a 12-byte
ObjectId). - You can provide your own
_id, or MongoDB generates one automatically. - Those 12 bytes break down into: 4 bytes for the timestamp, 3 bytes for the machine identifier, 2 bytes for the process ID, and 3 bytes as an incrementing counter.
- By default it's a 24-character hex string (a 12-byte
Common Errors & Troubleshooting
MongoServerError: connect ECONNREFUSED 127.0.0.1:27017— the MongoDB server isn't running locally. Start it (mongod, or your OS service manager), or double-check you're pointing at the right host/port if it's running elsewhere.- Confusing
_idwith a plain string —_idis stored as a specialObjectIdtype, not a string. Comparing it against a plain string ({ _id: "64ba88c..." }) silently matches nothing — wrap it inObjectId(...)(covered in the Utility Methods lesson). - Assuming a collection needs to be created first — unlike a SQL table, you don't need
createCollection()before inserting — the firstinsertOne()/insertMany()into a new collection name creates it automatically.
Key Takeaways
- MongoDB stores data as flexible BSON documents, not rows in tables.
- Database → Collection → Document maps to Database → Table → Row in the SQL world.
- Every document gets a unique
_id, which also encodes its creation time.
Advertisement320 x 100