|
| 1 | +// db.collection.aggregate() - Aggregation pipeline |
| 2 | + |
| 3 | +// Empty pipeline |
| 4 | +db.orders.aggregate([]) |
| 5 | + |
| 6 | +// Single stage pipelines |
| 7 | +db.orders.aggregate([{ $match: { status: "completed" } }]) |
| 8 | +db.orders.aggregate([{ $group: { _id: "$category", count: { $sum: 1 } } }]) |
| 9 | +db.orders.aggregate([{ $sort: { createdAt: -1 } }]) |
| 10 | +db.orders.aggregate([{ $limit: 10 }]) |
| 11 | +db.orders.aggregate([{ $skip: 20 }]) |
| 12 | +db.orders.aggregate([{ $project: { name: 1, total: 1, _id: 0 } }]) |
| 13 | +db.orders.aggregate([{ $unwind: "$items" }]) |
| 14 | +db.orders.aggregate([{ $count: "totalOrders" }]) |
| 15 | + |
| 16 | +// $match stage variations |
| 17 | +db.users.aggregate([{ $match: { age: { $gt: 18 } } }]) |
| 18 | +db.users.aggregate([{ $match: { status: { $in: ["active", "pending"] } } }]) |
| 19 | +db.users.aggregate([{ $match: { $or: [{ role: "admin" }, { role: "moderator" }] } }]) |
| 20 | +db.users.aggregate([{ $match: { "address.country": "USA" } }]) |
| 21 | + |
| 22 | +// $group stage variations |
| 23 | +db.orders.aggregate([{ $group: { _id: "$customerId", total: { $sum: "$amount" } } }]) |
| 24 | +db.orders.aggregate([{ $group: { _id: "$category", avgPrice: { $avg: "$price" } } }]) |
| 25 | +db.orders.aggregate([{ $group: { _id: "$status", count: { $sum: 1 }, items: { $push: "$name" } } }]) |
| 26 | +db.orders.aggregate([{ $group: { _id: null, totalRevenue: { $sum: "$amount" } } }]) |
| 27 | +db.sales.aggregate([{ $group: { _id: { year: { $year: "$date" }, month: { $month: "$date" } }, total: { $sum: "$amount" } } }]) |
| 28 | + |
| 29 | +// $project stage variations |
| 30 | +db.users.aggregate([{ $project: { name: 1, email: 1 } }]) |
| 31 | +db.users.aggregate([{ $project: { password: 0, ssn: 0 } }]) |
| 32 | +db.users.aggregate([{ $project: { fullName: { $concat: ["$firstName", " ", "$lastName"] } } }]) |
| 33 | +db.orders.aggregate([{ $project: { total: { $multiply: ["$price", "$quantity"] } } }]) |
| 34 | + |
| 35 | +// $sort stage variations |
| 36 | +db.users.aggregate([{ $sort: { name: 1 } }]) |
| 37 | +db.users.aggregate([{ $sort: { createdAt: -1 } }]) |
| 38 | +db.users.aggregate([{ $sort: { lastName: 1, firstName: 1 } }]) |
| 39 | + |
| 40 | +// $lookup stage (join) |
| 41 | +db.orders.aggregate([{ $lookup: { from: "users", localField: "customerId", foreignField: "_id", as: "customer" } }]) |
| 42 | +db.orders.aggregate([{ $lookup: { from: "products", localField: "productIds", foreignField: "_id", as: "products" } }]) |
| 43 | + |
| 44 | +// Multi-stage pipelines |
| 45 | +db.orders.aggregate([ |
| 46 | + { $match: { status: "completed" } }, |
| 47 | + { $group: { _id: "$customerId", total: { $sum: "$amount" } } }, |
| 48 | + { $sort: { total: -1 } }, |
| 49 | + { $limit: 10 } |
| 50 | +]) |
| 51 | + |
| 52 | +db.sales.aggregate([ |
| 53 | + { $match: { date: { $gte: ISODate("2024-01-01"), $lt: ISODate("2025-01-01") } } }, |
| 54 | + { $group: { |
| 55 | + _id: { year: { $year: "$date" }, month: { $month: "$date" } }, |
| 56 | + totalRevenue: { $sum: "$amount" }, |
| 57 | + avgOrderValue: { $avg: "$amount" }, |
| 58 | + orderCount: { $sum: 1 } |
| 59 | + } }, |
| 60 | + { $sort: { "_id.year": 1, "_id.month": 1 } } |
| 61 | +]) |
| 62 | + |
| 63 | +db.users.aggregate([ |
| 64 | + { $match: { status: "active" } }, |
| 65 | + { $lookup: { from: "orders", localField: "_id", foreignField: "customerId", as: "orders" } }, |
| 66 | + { $project: { name: 1, email: 1, orderCount: { $size: "$orders" } } }, |
| 67 | + { $sort: { orderCount: -1 } }, |
| 68 | + { $limit: 100 } |
| 69 | +]) |
| 70 | + |
| 71 | +// Pipeline with $addFields |
| 72 | +db.orders.aggregate([ |
| 73 | + { $addFields: { totalWithTax: { $multiply: ["$total", 1.1] } } } |
| 74 | +]) |
| 75 | + |
| 76 | +// Pipeline with $set (alias for $addFields) |
| 77 | +db.orders.aggregate([ |
| 78 | + { $set: { processed: true, processedAt: ISODate() } } |
| 79 | +]) |
| 80 | + |
| 81 | +// Pipeline with $unset |
| 82 | +db.users.aggregate([ |
| 83 | + { $unset: ["password", "ssn", "internalNotes"] } |
| 84 | +]) |
| 85 | + |
| 86 | +// Pipeline with $replaceRoot |
| 87 | +db.orders.aggregate([ |
| 88 | + { $replaceRoot: { newRoot: "$shipping" } } |
| 89 | +]) |
| 90 | + |
| 91 | +// Pipeline with $facet (multiple pipelines) |
| 92 | +db.products.aggregate([ |
| 93 | + { $facet: { |
| 94 | + categoryCounts: [{ $group: { _id: "$category", count: { $sum: 1 } } }], |
| 95 | + priceStats: [{ $group: { _id: null, avgPrice: { $avg: "$price" }, maxPrice: { $max: "$price" } } }] |
| 96 | + } } |
| 97 | +]) |
| 98 | + |
| 99 | +// Aggregate with options (options passed to driver) |
| 100 | +db.orders.aggregate([{ $match: { status: "completed" } }], { allowDiskUse: true }) |
| 101 | +db.orders.aggregate([{ $group: { _id: "$category" } }], { maxTimeMS: 60000 }) |
| 102 | +db.orders.aggregate([{ $sort: { total: -1 } }], { collation: { locale: "en" } }) |
| 103 | + |
| 104 | +// Aggregate with collection access patterns |
| 105 | +db["orders"].aggregate([{ $match: { status: "pending" } }]) |
| 106 | +db['audit-logs'].aggregate([{ $group: { _id: "$action", count: { $sum: 1 } } }]) |
| 107 | +db.getCollection("sales").aggregate([{ $match: { year: 2024 } }]) |
0 commit comments