
MongoDB Practical Beginner Guide: From Database Concepts to Node.js Integration
1. What MongoDB Is
MongoDB is often called a NoSQL database, but the better meaning is Not Only SQL, not “No SQL.”
The text explains that MongoDB is best understood as a document database. It stores data in documents that look like JSON, although internally MongoDB stores them as BSON.
Main idea:
MongoDB = Document Database
⸻
2. MongoDB vs Relational Databases
Relational databases store data like spreadsheets:
Tables → Rows → Columns
They need a strict schema before inserting data.
MongoDB works differently:
Database → Collection → Document
A document can store related data together, for example:
{
“title”: “Post One”,
“category”: “News”,
“tags”: [“MongoDB”, “Database”],
“likes”: 5
}
So instead of splitting data into many tables, MongoDB often keeps related data in one document.
⸻
3. JSON and BSON
Developers usually work with JSON-like data:
{
“name”: “Sara”,
“age”: 25
}
But MongoDB stores it internally as BSON.
BSON is like JSON, but with extra data types and better database performance.
⸻
4. Flexible Schema
MongoDB does not require a fixed schema by default.
That means documents in the same collection do not all need the exact same fields.
Example:
{ “name”: “Sara” }
and
{ “name”: “Kian”, “age”: 22, “skills”: [“Node.js”, “MongoDB”] }
can both be in the same collection.
⸻
5. MongoDB Hosting Options
The text explains two ways to use MongoDB:
Local MongoDB
You install MongoDB on your own computer or server.
Good for:
* Full control
* Local practice
But you must manage:
* Updates
* Server maintenance
* Security
MongoDB Atlas
Atlas is MongoDB’s cloud platform.
Good for:
* Easier setup
* Free tier
* No server maintenance
The tutorial chooses MongoDB Atlas.
⸻
6. Creating an Atlas Cluster
In Atlas, you create a cluster.
Cluster options include:
* Serverless → pay as you go
* Dedicated → professional/enterprise use
* Shared → free, good for small projects and testing
The tutorial chooses the shared free cluster.
⸻
7. Atlas Security Setup
MongoDB Atlas is locked by default.
You must configure two things:
Database Access
Create a database user with:
* Username
* Password
* Read/write permission
Network Access
Add an allowed IP address.
Important warning:
Allow access from anywhere = security risk
It may be okay for testing, but not for production.
⸻
8. Connecting with MongoDB Shell
The tutorial connects using mongosh.
Check version:
mongosh –version
Connect using Atlas connection string.
Basic commands:
db
show dbs
use blog
Important point:
A database may not appear in show dbs until you insert data into it.
⸻
9. Creating Databases and Collections
You can create a collection manually:
db.createCollection(“posts”)
Or MongoDB can create it automatically when inserting data:
db.posts.insertOne({…})
In this example:
blog = database
posts = collection
document = one blog post
⸻
10. Creating Documents
Insert one document
db.posts.insertOne({
title: “Post One”,
body: “This is a post”,
category: “News”,
likes: 1,
tags: [“news”, “mongodb”],
date: Date()
})
Insert many documents
db.posts.insertMany([
{ title: “Post Two”, category: “Tech” },
{ title: “Post Three”, category: “News” }
])
Important correction:
It is insertMany(), not addMany().
⸻
11. Reading Documents
Find all documents:
db.posts.find()
Find by category:
db.posts.find({ category: “News” })
Find one document:
db.posts.findOne({ title: “Post One” })
Count documents:
db.posts.find({ category: “News” }).count()
Limit results:
db.posts.find().limit(2)
Sort results:
db.posts.find().sort({ title: -1 })
⸻
12. Query Operators
The text introduces operators for filtering data.
Examples:
$gt // greater than
$gte // greater than or equal
$lt // less than
$lte // less than or equal
Example:
db.posts.find({ likes: { $gt: 3 } })
Meaning:
Find posts with more than 3 likes.
⸻
13. Updating Documents
Update one document:
db.posts.updateOne(
{ title: “Post One” },
{ $set: { category: “Tech” } }
)
Important:
Use $set to update only one field and keep the rest of the document.
⸻
14. Upsert
Upsert means:
Update if found
Insert if not found
Example:
db.posts.updateOne(
{ title: “Post Six” },
{ $set: { title: “Post Six”, category: “News” } },
{ upsert: true }
)
⸻
15. Incrementing Values
Use $inc to increase a number.
Example:
db.posts.updateOne(
{ title: “Post One” },
{ $inc: { likes: 2 } }
)
For all documents:
db.posts.updateMany(
{},
{ $inc: { likes: 1 } }
)
⸻
16. Deleting Documents
Delete one:
db.posts.deleteOne({ title: “Post Six” })
Delete many:
db.posts.deleteMany({ category: “Tech” })
Dangerous example:
db.posts.deleteMany({})
This deletes everything in the collection.
⸻
17. Viewing Data in Atlas
In Atlas, you can use Browse Collections to:
* View databases
* View collections
* Add documents manually
* Edit documents
* Filter data
The tutorial shows the blog database and posts collection.
⸻
18. MongoDB Compass
MongoDB Compass is the visual desktop app for MongoDB.
It can be used to:
* View data
* Query data
* Create databases
* Create collections
* Analyze indexes
* Build aggregation pipelines
⸻
19. Sample Data
The tutorial loads MongoDB sample data.
One example is:
sample_airbnb
It contains rental listings, like Airbnb data.
This is used for practicing real queries.
⸻
20. Aggregation Pipeline
Aggregation lets you filter and transform data step by step.
Example goal:
Find rental listings that:
* Accommodate more than 4 people
* Cost less than 500
* Include a hair dryer
* Are sorted by price
* Show only needed fields
* Limit results to 20
Pipeline stages:
$match
$sort
$project
$limit
Important:
The order matters.
Good order:
Match → Sort → Project → Limit
⸻
21. VS Code MongoDB Extension
The tutorial also connects MongoDB to VS Code.
With the extension, you can:
* View databases
* View collections
* Open documents
* Create playgrounds
* Run queries inside VS Code
This makes MongoDB easier for developers working inside a code editor.
⸻
22. MongoDB Playground
A playground is like a practice file for MongoDB commands.
You can:
* Select a database
* Drop a collection
* Insert test data
* Run find queries
* Run aggregation pipelines
Useful for learning and testing before writing backend code.
⸻
23. Connecting MongoDB to Node.js
The tutorial ends by connecting MongoDB to a Node.js app.
Setup:
npm init -y
npm i mongodb
Basic flow:
Import MongoDB package
→ Create MongoClient
→ Connect with URI
→ Select database
→ Select collection
→ Run query or aggregation
Important production note:
The password should be stored in an environment variable, not directly in the code.
⸻
24. Final Understanding
The full context teaches MongoDB in this order:
Concepts
→ Atlas setup
→ Security
→ Shell connection
→ CRUD
→ Atlas dashboard
→ Compass
→ Aggregation
→ VS Code
→ Node.js app
