This guide translates common SQL operations into MongoDB (NoSQL) syntax, highlighting the shift from tables and rows to collections and documents.
| File | Topic |
|---|---|
| 01-finding-data.md | Finding Data β SELECT vs .findOne() |
| 02-conditional-queries.md | Conditional Queries β Operators & Filtering |
| 03-projections.md | Projections β Selecting Specific Fields |
| 04-arrays.md | The NoSQL Superpower: Arrays |
| 05-joins-lookup.md | Relationships & Joins β $lookup |
| 06-aggregation.md | Complex Aggregation β The "Assembly Line" |
| 07-rules-of-thumb.md | Quick Rules of Thumb |
SQL MongoDB
βββββββββββββββββββββββββββββββββββββββββ
Database β Database
Table β Collection
Row β Document (JSON-like)
Column β Field
JOIN β $lookup (or embed!)
WHERE β filter object { field: value }
SELECT col β projection { col: 1 }
GROUP BY β $group (aggregation stage)
| SQL Thinking | MongoDB Thinking |
|---|---|
| Normalize data into many tables | Embed related data in one document |
| Rows are flat | Documents can have nested objects & arrays |
| Joins are cheap | Joins ($lookup) are expensive β avoid when possible |
| Schema is rigid (defined upfront) | Schema is flexible (per-document) |
A quick guide translating common SQL operations into MongoDB syntax.
SQL uses tables & rows, while MongoDB uses collections & documents.
Looking for one specific record, e.g., a user by email.
| Feature | SQL (Relational) | MongoDB (Document) |
|---|---|---|
| Concept | SELECT * with LIMIT 1 |
findOne() method |
SELECT *
FROM Users
WHERE email = 'jane@example.com'
LIMIT 1;db.users.findOne({ email: "jane@example.com" });Filtering results based on conditions.
SQL uses math symbols
>, <, =, >=
MongoDB uses operators
$gt Greater Than
$lt Less Than
$gte Greater Than or Equal
$lte Less Than or Equal
Find all active users older than 21
SELECT *
FROM Users
WHERE status = 'active'
AND age > 21;db.users.find({
status: "active",
age: { $gt: 21 }
});SQL selects columns directly.
MongoDB passes a projection object as the 2nd parameter.
1 = show field
0 = hide field
SELECT username, email
FROM Users
WHERE status = 'active';db.users.find(
{ status: "active" }, // Filter (WHERE)
{ username: 1, email: 1, _id: 0 } // Projection (SELECT)
);MongoDB makes querying arrays very easy.
Find blog posts tagged "Tutorial"
Requires join tables
Posts
Post_Tags
Tags
MongoDB automatically searches inside arrays.
db.posts.find({
tags: "Tutorial"
});Example document:
{
"title": "Learn MongoDB",
"tags": ["Database", "Tutorial", "Backend"]
}MongoDB equivalent of LEFT OUTER JOIN.
Uses the Aggregation Pipeline.
SQL joins flatten data
Order + 3 products
= 3 rows
MongoDB keeps hierarchy
1 order document
+ array of 3 products
SELECT *
FROM Orders
LEFT JOIN Products
ON Orders.product_id = Products.id;db.orders.aggregate([
{
$lookup: {
from: "products", // The collection to join with
localField: "product_id", // The field in the 'Orders' collection
foreignField: "_id", // The field in the 'Products' collection
as: "product_details" // What to name the resulting array
}
}
])MongoDB can chain operations like an assembly line.
Example: filter β join β calculate
db.orders.aggregate([
// 1οΈβ£ WHERE
{
$match: { customer: "Customer A" }
},
// 2οΈβ£ JOIN
{
$lookup: {
from: "products",
localField: "product_id",
foreignField: "_id",
as: "product_details"
}
},
// 3οΈβ£ GROUP BY
{
$group: {
_id: "$customer",
total: { $sum: "$price" }
}
}
])Use when data belongs to a single parent
Example
Post
βββ Comments
Example document
{
"title": "MongoDB Tips",
"comments": [
{ "user": "John", "text": "Nice post!" }
]
}Use when data is shared by many documents
Example
Orders β Products
Use $lookup.
If your MongoDB queries use $lookup everywhere, you are probably trying to build:
A SQL database inside MongoDB
MongoDB works best when data is embedded, not constantly joined.
| SQL | MongoDB |
|---|---|
| Database | Database |
| Table | Collection |
| Row | Document |
| Column | Field |
| JOIN | $lookup |
| GROUP BY | $group |
| WHERE | $match |
β Tip for SQL Developers
Think:
SQL = Normalize data
MongoDB = Denormalize data
MongoDB favors reading fast from one document instead of joining many tables.