Indexes and the Aggregation Pipeline
Why indexes
Without an index, MongoDB scans every document in a collection to answer a query – fine for ten documents, disastrous for ten million. An index is a sorted data structure that lets MongoDB jump straight to matching documents.
db.users.createIndex({ email: 1 }, { unique: true })
db.products.createIndex({ category: 1, price: -1 }) // compound index
Add { unique: true } to also enforce uniqueness (great for emails). Verify a query uses an index with .explain("executionStats").
The aggregation pipeline
Aggregation processes documents through a pipeline of stages, each transforming the stream and passing it to the next – like Unix pipes for data. The workhorses are $match (filter), $group (summarise) and $sort.
db.orders.aggregate([
{ $match: { status: "paid" } },
{ $group: { _id: "$city", total: { $sum: "$amount" }, count: { $sum: 1 } } },
{ $sort: { total: -1 } }
])
Read it top to bottom: keep only paid orders, group them by city summing the amount and counting orders, then sort cities by total revenue. This is the equivalent of SQL’s GROUP BY with SUM and ORDER BY.
More useful stages
$project– reshape documents, add computed fields.$limit/$skip– paginate results.$lookup– join another collection.
Key points
- Indexes turn full scans into fast lookups; add
unique: trueto enforce uniqueness. - Use
.explain()to confirm a query is using an index. - Aggregation runs documents through ordered stages –
$match,$group,$sort. $groupwith$sumis MongoDB’s answer to SQLGROUP BY.