084
LVL 03 — MID DEVELOPERSESSION 084DAY 84

MONGODB AGGREGATION

🎫 PIXELCRAFT-071
Feature | 🟠 Hard | Priority: 🟠 High

The analytics dashboard needs deeper insights: most popular filters by month, average image size trends. And account deletion must be atomic — delete user + all images + all events in a single transaction. All or nothing.
CONCEPTS.UNLOCKED
🔄
Aggregation Pipeline
A series of stages that transform data. Documents flow through stages like a factory line: filter → group → sort → reshape. MongoDB's answer to SQL's GROUP BY + JOIN + subqueries.
🔍
$match & $group
Filter, then aggregate. $match narrows the dataset (like WHERE). $group collects documents and computes: $sum, $avg, $min, $max, $push. The two most-used stages.
📊
$sort & $project
Order and reshape. $sort orders results. $project selects fields and computes new ones — rename, calculate, format dates. Control the exact shape of your output.
🔗
$lookup
MongoDB's version of JOIN. Pull related documents from another collection into the pipeline. "Get each user's images" without multiple queries. Left outer join by default.
🔒
Transactions
All-or-nothing operations. Start a session, perform multiple writes, commit or abort. If any step fails, everything rolls back. Essential for data integrity across collections.
⚛️
Atomic Operations
Single-document operations are always atomic. updateOne with $inc, $push, $set — guaranteed consistent. Multi-document atomicity requires transactions (MongoDB 4.0+).
HANDS-ON.TASKS
01
Popular Filters by Month
const popularFilters = await AnalyticsEvent.aggregate([ // Stage 1: Only filter events { $match: { eventType: 'filter_apply', createdAt: { $gte: sixMonthsAgo } }}, // Stage 2: Group by month + filter { $group: { _id: { month: { $dateToString: { format: '%Y-%m', date: '$createdAt' }}, filter: '$metadata.filterName' }, count: { $sum: 1 } }}, // Stage 3: Sort by month, then count { $sort: { '_id.month': -1, count: -1 }}, // Stage 4: Reshape output { $project: { _id: 0, month: '$_id.month', filter: '$_id.filter', count: 1 }} ]);
02
Average Image Size Trends
const sizeTrends = await Image.aggregate([ { $match: { createdAt: { $gte: threeMonthsAgo } }}, { $group: { _id: { $dateToString: { format: '%Y-%m-%d', date: '$createdAt' }}, avgSize: { $avg: '$fileSize' }, totalSize: { $sum: '$fileSize' }, count: { $sum: 1 }, maxSize: { $max: '$fileSize' }, }}, { $sort: { _id: 1 } }, { $project: { _id: 0, date: '$_id', avgSizeMB: { $round: [{ $divide: ['$avgSize', 1048576] }, 2] }, totalSizeMB: { $round: [{ $divide: ['$totalSize', 1048576] }, 2] }, count: 1, }} ]);
03
$lookup: Users with Their Images
const usersWithStats = await User.aggregate([ { $lookup: { from: 'images', localField: '_id', foreignField: 'owner', as: 'images' }}, { $project: { name: 1, email: 1, imageCount: { $size: '$images' }, totalSize: { $sum: '$images.fileSize' }, joinedAt: '$createdAt', }}, { $sort: { imageCount: -1 } }, { $limit: 20 }, ]);
04
Transaction: Atomic Account Deletion
async function deleteAccount(userId) { const session = await mongoose.startSession(); try { session.startTransaction(); // Delete all user's images await Image.deleteMany( { owner: userId }, { session }); // Delete all analytics events await AnalyticsEvent.deleteMany( { userId }, { session }); // Delete the user await User.findByIdAndDelete( userId, { session }); // All succeeded → commit await session.commitTransaction(); return { success: true }; } catch (error) { // Any failure → rollback ALL await session.abortTransaction(); throw error; } finally { session.endSession(); } }
Without a transaction, if deleting events fails after deleting images, you'd have orphaned data. With a transaction: either everything deletes, or nothing does. Data stays consistent.
05
Close the Ticket
git switch -c feature/PIXELCRAFT-071-aggregation git add server/ git commit -m "Add aggregation pipeline + transactions (PIXELCRAFT-071)" git push origin feature/PIXELCRAFT-071-aggregation # PR → Review → Merge → Close ticket ✅
CS.DEEP-DIVE

ACID properties are the foundation of reliable databases.

Transactions guarantee ACID — the properties that prevent your data from becoming corrupted, even during crashes.

// ACID properties:

Atomicity
  All operations succeed or
  all are rolled back. No partial state.

Consistency
  Data moves from one valid state
  to another. Constraints are enforced.

Isolation
  Concurrent transactions don't
  interfere with each other.

Durability
  Once committed, data survives
  crashes, power loss, disasters.

// MongoDB: single-doc ACID always.
// Multi-doc transactions since 4.0.
// PostgreSQL: full ACID since 1996.
// Choose based on your data model.
"Aggregation Lab"
[A]Build a user retention cohort analysis: group users by signup week, track what % are still active 1, 2, 3, 4 weeks later. Display as a cohort table in the analytics dashboard.
[B]Add $facet for multi-dimension analytics: in a single aggregation, compute top filters, upload trends, AND active users simultaneously. Return all three datasets in one query.
[C]Research: MongoDB aggregation pipeline vs SQL analytical queries. Rewrite each aggregation as equivalent SQL. Compare readability, performance, and expressiveness.
REF.MATERIAL
ARTICLE
MongoDB
Official aggregation reference: all stages, operators, expressions, and optimization. The definitive guide to MongoDB data processing.
MONGODBOFFICIALESSENTIAL
VIDEO
Fireship
Visual walkthrough of aggregation stages: $match, $group, $sort, $lookup, $unwind. Practical examples with real data.
AGGREGATIONQUICK
ARTICLE
MongoDB
Multi-document transactions: sessions, commit, abort, read/write concerns. ACID guarantees across collections and databases.
TRANSACTIONSOFFICIAL
ARTICLE
Wikipedia
The theory behind database transactions: atomicity, consistency, isolation, durability. The CS foundation of reliable data systems.
ACIDTHEORYCS
VIDEO
Traversy Media
Comprehensive MongoDB tutorial including aggregation pipeline, indexing, and Mongoose. Hands-on with real-world examples.
MONGODBTUTORIAL
// LEAVE EXCITED BECAUSE
The analytics dashboard now shows filter popularity by month, image size trends, and user rankings — all from aggregation pipelines. Account deletion is atomic: all or nothing. Your data layer is production-grade.