Skip to content

Instantly share code, notes, and snippets.

@powertech2nd
Last active March 5, 2026 11:53
Show Gist options
  • Select an option

  • Save powertech2nd/4cf5a323387587ff534d295f40255f7a to your computer and use it in GitHub Desktop.

Select an option

Save powertech2nd/4cf5a323387587ff534d295f40255f7a to your computer and use it in GitHub Desktop.
NoSQL tutorial

πŸ“Š SQL vs. MongoDB: The Ultimate Cheat Sheet

This guide translates common SQL operations into MongoDB (NoSQL) syntax, highlighting the shift from tables and rows to collections and documents.


πŸ“ Sections

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

πŸ—ΊοΈ Core Concept Map

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)

⚑ Key Mindset Shift

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)



πŸ“Š SQL vs MongoDB β€” Ultimate Cheat Sheet [TLDR]

A quick guide translating common SQL operations into MongoDB syntax.

SQL uses tables & rows, while MongoDB uses collections & documents.


1️⃣ Finding Data (The Basics)

Looking for one specific record, e.g., a user by email.

Feature SQL (Relational) MongoDB (Document)
Concept SELECT * with LIMIT 1 findOne() method

SQL

SELECT * 
FROM Users 
WHERE email = 'jane@example.com'
LIMIT 1;

MongoDB

db.users.findOne({ email: "jane@example.com" });

2️⃣ Conditional Queries (Find Many)

Filtering results based on conditions.

Logic

SQL uses math symbols

>, <, =, >=

MongoDB uses operators

$gt   Greater Than
$lt   Less Than
$gte  Greater Than or Equal
$lte  Less Than or Equal

Task

Find all active users older than 21

SQL

SELECT *
FROM Users
WHERE status = 'active'
AND age > 21;

MongoDB

db.users.find({
  status: "active",
  age: { $gt: 21 }
});

3️⃣ Projections (Selecting Columns)

SQL selects columns directly.

MongoDB passes a projection object as the 2nd parameter.

Rules

1 = show field
0 = hide field

SQL

SELECT username, email
FROM Users
WHERE status = 'active';

MongoDB

db.users.find(
  { status: "active" },              // Filter (WHERE)
  { username: 1, email: 1, _id: 0 }  // Projection (SELECT)
);

4️⃣ The NoSQL Superpower: Arrays

MongoDB makes querying arrays very easy.

Task

Find blog posts tagged "Tutorial"

SQL Approach

Requires join tables

Posts
Post_Tags
Tags

MongoDB

MongoDB automatically searches inside arrays.

db.posts.find({
  tags: "Tutorial"
});

Example document:

{
  "title": "Learn MongoDB",
  "tags": ["Database", "Tutorial", "Backend"]
}

5️⃣ Relationships & Joins ($lookup)

MongoDB equivalent of LEFT OUTER JOIN.

Uses the Aggregation Pipeline.

Result Difference

SQL joins flatten data

Order + 3 products
= 3 rows

MongoDB keeps hierarchy

1 order document
+ array of 3 products

SQL

SELECT *
FROM Orders
LEFT JOIN Products
ON Orders.product_id = Products.id;

MongoDB

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
    }
  }
])

6️⃣ Aggregation Pipeline (Complex Queries)

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" }
    }
  }

])

πŸ’‘ Quick Rules of Thumb

Embed Data

Use when data belongs to a single parent

Example

Post
 └── Comments

Example document

{
  "title": "MongoDB Tips",
  "comments": [
    { "user": "John", "text": "Nice post!" }
  ]
}

Reference Data

Use when data is shared by many documents

Example

Orders β†’ Products

Use $lookup.


🚩 Red Flag

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.


⚑ Mental Model

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment