DEV Community

Cover image for MongoDB Made Easy: A Beginner’s Practical Guide (Part 1)
Ahmed Afzal
Ahmed Afzal

Posted on

MongoDB Made Easy: A Beginner’s Practical Guide (Part 1)

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()
}
Enter fullscreen mode Exit fullscreen mode
  • _id is auto-generated by MongoDB using ObjectId. 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" }
]
Enter fullscreen mode Exit fullscreen mode

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.”

MongoDB Download page

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
Enter fullscreen mode Exit fullscreen mode

“📸 Below is the output you’ll see if MongoDB is installed correctly.”

MongoDB shell output after running a command using mongosh

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
Enter fullscreen mode Exit fullscreen mode

You'll be connected to the local MongoDB instance.

test> use blogDB
Enter fullscreen mode Exit fullscreen mode

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()
})
Enter fullscreen mode Exit fullscreen mode

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.

MongoDB shell output after inserting a document using insertOne()

➕ 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"] }
])
Enter fullscreen mode Exit fullscreen mode

MongoDB shell output after inserting a documents using insertMany()

🔍 Finding & Filtering Data

📄 View All Documents

db.articles.find().pretty()
Enter fullscreen mode Exit fullscreen mode
  • .find() = get all documents
  • .pretty() = display in a clean format

“Below you can see all the inserted data displayed in a clean format”

MongoDB shell output after displaying a document using find()

🔎 Filtering

// Find all articles by Ahmed
db.articles.find({ author: "Ahmed" })

// Find all published articles
db.articles.find({ published: true })
Enter fullscreen mode Exit fullscreen mode

“📸 Here’s an example of how filtered data appears using the find command.”

MongoDB shell output after filtering a document using find()

🧠 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 })
Enter fullscreen mode Exit fullscreen mode

🔄 Updating Documents

✏️ updateOne()

db.articles.updateOne(
  { author: "Ahmed" },
  { $set: { published: false } }
)
Enter fullscreen mode Exit fullscreen mode

Updates the first article by Ahmed.

“📸 Below is the terminal output after running an updateOne operation.”

MongoDB shell output after updating a document using iupdateOne()

✏️ updateMany()

db.articles.updateMany(
  { published: true },
  { $set: { category: "Tech" } }
)
Enter fullscreen mode Exit fullscreen mode

Updates all published articles.

“📸 Below is the terminal output after running an updateMany operation.”

MongoDB shell output after updating all documents using updateMany()

🔧 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" })
Enter fullscreen mode Exit fullscreen mode

Deletes the first document where author is Ahmed.

“📸 Below is the terminal output after running an deleteOne operation.”

MongoDB shell output after deleting a document using deleteOne()

deleteMany()

db.articles.deleteMany({ published: false })
Enter fullscreen mode Exit fullscreen mode

Deletes all documents where published is false.

⚠️ deleteMany({}) will delete everything in the collection — use with care!


🆔 Working with ObjectId

ObjectId("66a3f20c8ae6c5f5f9637a5d")
Enter fullscreen mode Exit fullscreen mode
  • 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") })
Enter fullscreen mode Exit fullscreen mode

MongoDB shell output after perfoming operation using _id

🎯 Above you can review how _id helps in exact targeting of operations.


🧭 Using MongoDB Compass (GUI Method)

Creating a new connection in MongoDB Compass

✅ 1. Open Compass & Connect

Click on Add New Connection

Paste: mongodb://localhost:27017

than click Save & Connect

Connect at mongodb://localhost:27017 in MongoDB Compass

✅ 2. Create Database & Collection

  • Click “Create Database”
  • Name: blogDB, Collection: articles

Creating a new database and collection in MongoDB Compass

✅ 3. Insert Document

Click “Insert Document” → Paste your JSON → Click Insert

Inserting a new document using MongoDB Compass interface

✅ 4. Read/View Documents

Documents will show in table view. Click {} to view raw JSON.

MongoDB Compass View data after inserting a document

✅ 5. Update Document

Click ✏️ icon → Edit → REPLACE✅

Update Document in MongoDB Compass

✅ 6. Delete Document

Click 🗑️ icon → DELETE

Delete Document in MongoDB Compass

📸 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)