|
| 1 | +/** |
| 2 | + * Aggregation pipeline example. |
| 3 | + * See: docs/aggregate.md |
| 4 | + * |
| 5 | + * Run: |
| 6 | + * npm run build && tsc -p tsconfig.examples.json |
| 7 | + * node .generated/examples-dist/examples/docs/aggregate.js |
| 8 | + */ |
| 9 | +import { setupExample, teardownExample } from '../helpers/bootstrap.js'; |
| 10 | + |
| 11 | +async function main() { |
| 12 | + const { msq, server } = await setupExample('example-aggregate'); |
| 13 | + const orders = msq.collection('orders'); |
| 14 | + |
| 15 | + await orders.insertMany([ |
| 16 | + { product: 'Widget', category: 'tools', amount: 29.99, qty: 3, status: 'completed' }, |
| 17 | + { product: 'Gadget', category: 'tools', amount: 59.99, qty: 1, status: 'completed' }, |
| 18 | + { product: 'Widget', category: 'tools', amount: 29.99, qty: 2, status: 'pending' }, |
| 19 | + { product: 'Doohickey', category: 'parts', amount: 9.99, qty: 5, status: 'completed' }, |
| 20 | + { product: 'Gadget', category: 'tools', amount: 59.99, qty: 2, status: 'completed' }, |
| 21 | + ]); |
| 22 | + |
| 23 | + // ── Basic aggregate pipeline ─────────────────────────────────────────── |
| 24 | + const revenueByCategory = await orders.aggregate([ |
| 25 | + { $match: { status: 'completed' } }, |
| 26 | + { |
| 27 | + $group: { |
| 28 | + _id: '$category', |
| 29 | + totalRevenue: { $sum: { $multiply: ['$amount', '$qty'] } }, |
| 30 | + orderCount: { $sum: 1 }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + { $sort: { totalRevenue: -1 } }, |
| 34 | + ]); |
| 35 | + console.log('Revenue by category:'); |
| 36 | + for (const cat of revenueByCategory as Record<string, unknown>[]) { |
| 37 | + console.log(` ${cat._id}: $${(cat.totalRevenue as number).toFixed(2)} (${cat.orderCount} orders)`); |
| 38 | + } |
| 39 | + |
| 40 | + // ── Aggregate with project + addFields ──────────────────────────────── |
| 41 | + const detailed = await orders.aggregate([ |
| 42 | + { $match: { status: 'completed' } }, |
| 43 | + { |
| 44 | + $addFields: { |
| 45 | + lineTotal: { $multiply: ['$amount', '$qty'] }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + { |
| 49 | + $project: { |
| 50 | + product: 1, |
| 51 | + qty: 1, |
| 52 | + amount: 1, |
| 53 | + lineTotal: 1, |
| 54 | + _id: 0, |
| 55 | + }, |
| 56 | + }, |
| 57 | + { $sort: { lineTotal: -1 } }, |
| 58 | + { $limit: 3 }, |
| 59 | + ]); |
| 60 | + console.log('\nTop 3 line items:'); |
| 61 | + for (const item of detailed as Record<string, unknown>[]) { |
| 62 | + console.log(` ${item.product} × ${item.qty} = $${(item.lineTotal as number).toFixed(2)}`); |
| 63 | + } |
| 64 | + |
| 65 | + // ── Aggregate with $count ────────────────────────────────────────────── |
| 66 | + const total = await orders.aggregate([ |
| 67 | + { $match: { status: 'completed' } }, |
| 68 | + { $count: 'completedOrders' }, |
| 69 | + ]); |
| 70 | + console.log('\nCompleted orders:', (total[0] as Record<string, unknown>)?.completedOrders); |
| 71 | + |
| 72 | + await teardownExample(msq, server); |
| 73 | + console.log('✅ Aggregate example complete'); |
| 74 | +} |
| 75 | + |
| 76 | +main().catch((err) => { |
| 77 | + console.error('❌ Example failed:', err); |
| 78 | + process.exit(1); |
| 79 | +}); |
0 commit comments