Welcome to your first step into the world of MongoDB! If you’ve ever felt overwhelmed by traditional SQL databases, or you're looking for something more flexible for your next project — you’re in the right place. In this guide, we’ll break down MongoDB in a super simple, practical, and beginner-friendly way. By the end of this article, you'll not only understand MongoDB, but you'll also perform real CRUD operations using both code and visual tools.
🚀 What is MongoDB?
MongoDB is an open-source, document-oriented NoSQL database. Unlike traditional relational databases that store data in tables, rows, and columns, MongoDB stores data in documents and collections, making it more flexible and scalable.
Each document in MongoDB is a JSON-like structure (called BSON internally), which allows you to store structured or unstructured data as key-value pairs. BSON makes it easier for machines to understand and process data quickly.
📄 Understanding MongoDB Structure
🧾 Document:
A document is a single unit of data in MongoDB, similar to a row in SQL but stored in a JSON-like format.
{
_id: "abc123",
title: "Post Title 1",
body: "Body of post.",
category: "News",
likes: 1,
tags: ["news", "events"],
date: new Date()
}
-
_id
is auto-generated by MongoDB usingObjectId
. You can use it to fetch, update, or delete that specific document.
📦 Collection:
A collection is a group of documents, like an array of objects or a table in SQL.
// Collection: articles
[
{ title: "Post 1", author: "Ahmed" },
{ title: "Post 2", author: "Sara" }
]
Each collection can store multiple documents with different structures.
💻 Installing MongoDB Locally
1️⃣ Go to MongoDB Download Page
👉 https://www.mongodb.com/try/download/community
2️⃣ Select Platform
Choose Windows, macOS, or Linux. For Windows:
- Version: MongoDB Community Server
- OS: Windows
- Package: MSI Installer
Click Download.
“Below you can see the MongoDB download page and installer setup selection.”
3️⃣ Install MongoDB
Run the downloaded installer:
- Choose Complete
- ✅ Check “Install MongoDB Compass” (GUI tool)
- Finish setup
4️⃣ Verify Installation
Open your terminal or command prompt:
mongosh
“📸 Below is the output you’ll see if MongoDB is installed correctly.”
If it opens the MongoDB shell, you're all set!
5️⃣ Optional: Install MongoDB Compass
Download: https://www.mongodb.com/try/download/compass
Compass provides a graphical interface to view and manipulate your data — like phpMyAdmin for MongoDB.
🧪 Creating a Database and Collection
Launch your terminal and type:
mongosh
You'll be connected to the local MongoDB instance.
test> use blogDB
This switches to blogDB
or creates it when you insert data.
➕ Insert One Document
db.articles.insertOne({
title: "How to Use MongoDB",
author: "Ahmed Afzal",
content: "This is my first blog post.",
category: "Database",
tags: ["mongodb", "beginner"],
published: true,
date: new Date()
})
Breakdown:
-
db
= current database -
articles
= collection -
insertOne
= insert 1 document
📸 Below you can see the image showing how MongoDB confirms the inserted document with an ObjectId.
➕ Insert Multiple Documents
db.articles.insertMany([
{ title: "Post 1", author: "Ahmed", tags: ["tech", "mongodb"] },
{ title: "Post 2", author: "Sara", tags: ["javascript", "nodejs"] },
{ title: "Post 3", author: "Ali", tags: ["nosql", "database"] }
])
🔍 Finding & Filtering Data
📄 View All Documents
db.articles.find().pretty()
-
.find()
= get all documents -
.pretty()
= display in a clean format
“Below you can see all the inserted data displayed in a clean format”
🔎 Filtering
// Find all articles by Ahmed
db.articles.find({ author: "Ahmed" })
// Find all published articles
db.articles.find({ published: true })
“📸 Here’s an example of how filtered data appears using the find command.”
🧠 Projections (Show/Hide Fields)
// Show only title and author
db.articles.find(
{ published: true },
{ title: 1, author: 1, _id: 0 }
)
// Hide 'content' field
db.articles.find({}, { content: 0 })
🔄 Updating Documents
✏️ updateOne()
db.articles.updateOne(
{ author: "Ahmed" },
{ $set: { published: false } }
)
Updates the first article by Ahmed.
“📸 Below is the terminal output after running an updateOne operation.”
✏️ updateMany()
db.articles.updateMany(
{ published: true },
{ $set: { category: "Tech" } }
)
Updates all published articles.
“📸 Below is the terminal output after running an updateMany operation.”
🔧 Common Update Operators
-
$set
→ Set or update a value -
$inc
→ Increment a number -
$unset
→ Remove a field -
$push
→ Add to array -
$addToSet
→ Add if not already present
🗑️ Deleting Documents
❌ deleteOne()
db.articles.deleteOne({ author: "Ahmed" })
Deletes the first document where author is Ahmed.
“📸 Below is the terminal output after running an deleteOne operation.”
❌ deleteMany()
db.articles.deleteMany({ published: false })
Deletes all documents where published
is false.
⚠️ deleteMany({})
will delete everything in the collection — use with care!
🆔 Working with ObjectId
ObjectId("66a3f20c8ae6c5f5f9637a5d")
- Used to uniquely identify documents
🧭 Use ObjectId in Queries
// Find by ID
db.articles.find({ _id: ObjectId("66a3f20c8ae6c5f5f9637a5d") })
// Update by ID
db.articles.updateOne(
{ _id: ObjectId("66a3f20c8ae6c5f5f9637a5d") },
{ $set: { title: "Updated Title" } }
)
// Delete by ID
db.articles.deleteOne({ _id: ObjectId("66a3f20c8ae6c5f5f9637a5d") })
🎯 Above you can review how _id
helps in exact targeting of operations.
🧭 Using MongoDB Compass (GUI Method)
✅ 1. Open Compass & Connect
Click on Add New Connection
Paste: mongodb://localhost:27017
than click Save & Connect
✅ 2. Create Database & Collection
- Click “Create Database”
- Name:
blogDB
, Collection:articles
✅ 3. Insert Document
Click “Insert Document” → Paste your JSON → Click Insert
✅ 4. Read/View Documents
Documents will show in table view. Click {}
to view raw JSON.
✅ 5. Update Document
Click ✏️ icon → Edit → REPLACE✅
✅ 6. Delete Document
Click 🗑️ icon → DELETE
📸 Above you can see a visual walk-through of inserting, updating, and deleting using Compass
🎯 Wrapping Up
Well done! You’ve successfully completed your first hands-on experience with MongoDB. From understanding core concepts to performing CRUD operations using both the shell and Compass, you now have a strong foundation to build upon.
In the next part of this series, we’ll explore advanced MongoDB topics including optimized queries, indexing strategies, relationships, and best practices for data modeling.
📌 Stay connected, and consider bookmarking this series to continue your learning journey.
🙏 As a continuous learner, I welcome your suggestions or feedback to improve this guide and support others on the same path.
Top comments (0)