MongoDB Database, Collection & Document – Core Concepts | MongoDB Zero to Hero (Day 4)
In this article, we will understand the three most important building blocks of MongoDB: Database, Collection, and Document.
If you understand these concepts clearly, MongoDB becomes very easy.
1. MongoDB Hierarchy
MongoDB structure follows this order:
- Database
- Collection
- Document
2. What is a Database?
A database is a container that stores collections.
Example:
- school
- company
- ecommerce
Create Database:
use school
Output:
switched to db school
3. What is a Collection?
A collection is similar to a table in SQL. It stores multiple documents.
Create Collection:
db.createCollection("students")
Now the collection is ready.
4. What is a Document?
A document is a single record in MongoDB. It is stored in JSON format.
Example Document:
{
name: "Ravi",
age: 21,
course: "BCA"
}
5. Insert a Document
db.students.insertOne({
name: "Anu",
age: 22,
course: "BSc"
})
Output:
acknowledged: true
6. View Documents
db.students.find()
7. Real-Life Analogy
School
- School → Database
- Class → Collection
- Student → Document
E-Commerce
- Website → Database
- Products → Collection
- Product → Document
8. Show All Databases
show dbs
9. Show All Collections
show collections
10. SQL vs MongoDB Mapping
SQL
- Database
- Table
- Row
MongoDB
- Database
- Collection
- Document
Conclusion
In this article, you learned:
- What is Database
- What is Collection
- What is Document
- How to create them
- Real-life examples
These are the core concepts of MongoDB. Every operation will use these three.
In the next article, we will learn MongoDB CRUD Operations – Insert & Read Data.
Your MongoDB Zero to Hero journey is becoming strong step by step 💪