MongoDB for SQL Developers

matt
Matthew Gros · Sep 25, 2025

TLDR

Embed related data, denormalize intentionally, design for your queries, use aggregation pipeline.

MongoDB for SQL Developers

Documents, Not Tables

Think differently about your data.

SQL vs MongoDB

-- SQL
SELECT * FROM users WHERE age > 21;

-- MongoDB
db.users.find({ age: { $gt: 21 } })

Document Structure

// Embedded (denormalized)
{
    _id: ObjectId("..."),
    name: "John",
    orders: [
        { product: "Widget", price: 9.99 },
        { product: "Gadget", price: 19.99 }
    ]
}

// Referenced (normalized)
{
    _id: ObjectId("..."),
    name: "John",
    orderIds: [ObjectId("..."), ObjectId("...")]
}

When to Embed

  • Data is always accessed together
  • One-to-few relationship
  • Data doesn't change often

When to Reference

  • Data accessed independently
  • One-to-many or many-to-many
  • Data changes frequently

Common Operations

// Insert
db.users.insertOne({ name: "John", age: 30 })

// Find
db.users.find({ age: { $gte: 21 } })
db.users.findOne({ _id: ObjectId("...") })

// Update
db.users.updateOne(
    { _id: ObjectId("...") },
    { $set: { age: 31 } }
)

// Delete
db.users.deleteOne({ _id: ObjectId("...") })

Aggregation Pipeline

db.orders.aggregate([
    { $match: { status: "completed" } },
    { $group: {
        _id: "$userId",
        total: { $sum: "$amount" }
    }},
    { $sort: { total: -1 } }
])

Indexing

// Single field
db.users.createIndex({ email: 1 })

// Compound
db.orders.createIndex({ userId: 1, createdAt: -1 })

// Text search
db.posts.createIndex({ title: "text", content: "text" })

About the Author

matt

I build and ship automation-driven products using Laravel and modern frontend stacks (Vue/React), with a focus on scalability, measurable outcomes, and tight user experience. I’m based in Toronto, have 13+ years in PHP, and I also hold a pilot’s license. I enjoy working on new tech projects and generally exploring new technology.