MongoDB Crash Course for SQL Developers
Thinking in Documents. Aggregation Pipelines, Indexes, and why 'Schemaless' doesn't mean structureless.
In SQL, you normalize data into separate tables. In MongoDB, you embed data.
Embedding vs Referencing
SQL (Reference):
Users table and Addresses table. Join them when querying.
MongoDB (Embed):
{
"_id": 1,
"name": "Abyan",
"address": {
"street": "123 Main St",
"city": "Jakarta"
}
}
Store data together that is accessed together. This makes reads incredibly fast (no joins needed).
The Aggregation Pipeline
Mongo's powerful alternative to complex SQL queries.
db.orders.aggregate([
{ $match: { status: "A" } },
{ $group: { _id: "$cust_id", total: { $sum: "$amount" } } }
])
It's like a Unix pipe for your data. Filter -> Group -> Sort -> Project.