|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const setupDatabase = require('../functional/shared').setupDatabase; |
| 4 | +const MongoClient = require('../../lib/mongo_client'); |
| 5 | + |
| 6 | +describe('examples.aggregaton:', function() { |
| 7 | + let client; |
| 8 | + let collection; |
| 9 | + |
| 10 | + before(async function() { |
| 11 | + await setupDatabase(this.configuration); |
| 12 | + }); |
| 13 | + |
| 14 | + beforeEach(async function() { |
| 15 | + client = await MongoClient.connect(this.configuration.url()); |
| 16 | + collection = client.db(this.configuration.db).collection('aggregateExample'); |
| 17 | + }); |
| 18 | + |
| 19 | + afterEach(async function() { |
| 20 | + await client.close(); |
| 21 | + client = undefined; |
| 22 | + collection = undefined; |
| 23 | + }); |
| 24 | + |
| 25 | + it('supports simple aggregation', { |
| 26 | + metadata: { requires: { mongodb: '>=2.8.0', topology: ['single'] } }, |
| 27 | + test: async function() { |
| 28 | + // Start aggregate example 1 |
| 29 | + await collection |
| 30 | + .aggregate([{ $match: { 'items.fruit': 'banana' } }, { $sort: { date: 1 } }]) |
| 31 | + .toArray(); |
| 32 | + // End aggregate example 1 |
| 33 | + } |
| 34 | + }); |
| 35 | + |
| 36 | + it('supports $match, $group, $project, $unwind, $sum, $sort, $dayOfWeek', { |
| 37 | + metadata: { requires: { mongodb: '>=2.8.0', topology: ['single'] } }, |
| 38 | + test: async function() { |
| 39 | + // Start aggregate example 2 |
| 40 | + await collection |
| 41 | + .aggregate([ |
| 42 | + { |
| 43 | + $unwind: '$items' |
| 44 | + }, |
| 45 | + { |
| 46 | + $match: { |
| 47 | + 'items.fruit': 'banana' |
| 48 | + } |
| 49 | + }, |
| 50 | + { |
| 51 | + $group: { |
| 52 | + _id: { day: { $dayOfWeek: '$date' } }, |
| 53 | + count: { $sum: '$items.quantity' } |
| 54 | + } |
| 55 | + }, |
| 56 | + { |
| 57 | + $project: { |
| 58 | + dayOfWeek: '$_id.day', |
| 59 | + numberSold: '$count', |
| 60 | + _id: 0 |
| 61 | + } |
| 62 | + }, |
| 63 | + { |
| 64 | + $sort: { numberSold: 1 } |
| 65 | + } |
| 66 | + ]) |
| 67 | + .toArray(); |
| 68 | + // End aggregate example 2 |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + it('supports $unwind, $group, $sum, $dayOfWeek, $multiply, $project, $cond', { |
| 73 | + metadata: { requires: { mongodb: '>=2.8.0', topology: ['single'] } }, |
| 74 | + test: async function() { |
| 75 | + // Start aggregate example 3 |
| 76 | + await collection |
| 77 | + .aggregate([ |
| 78 | + { |
| 79 | + $unwind: '$items' |
| 80 | + }, |
| 81 | + { |
| 82 | + $group: { |
| 83 | + _id: { day: { $dayOfWeek: '$date' } }, |
| 84 | + items_sold: { $sum: '$items.quantity' }, |
| 85 | + revenue: { $sum: { $multiply: ['$items.quantity', '$items.price'] } } |
| 86 | + } |
| 87 | + }, |
| 88 | + { |
| 89 | + $project: { |
| 90 | + day: '$_id.day', |
| 91 | + revenue: 1, |
| 92 | + items_sold: 1, |
| 93 | + discount: { |
| 94 | + $cond: { if: { $lte: ['$revenue', 250] }, then: 25, else: 0 } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + ]) |
| 99 | + .toArray(); |
| 100 | + // End aggregate example 3 |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + it('supports $lookup, $filter, $match', { |
| 105 | + metadata: { requires: { mongodb: '>=2.8.0', topology: ['single'] } }, |
| 106 | + test: async function() { |
| 107 | + // Start aggregate example 4 |
| 108 | + await collection |
| 109 | + .aggregate([ |
| 110 | + { |
| 111 | + $lookup: { |
| 112 | + from: 'air_airlines', |
| 113 | + let: { constituents: '$airlines' }, |
| 114 | + pipeline: [ |
| 115 | + { |
| 116 | + $match: { $expr: { $in: ['$name', '$$constituents'] } } |
| 117 | + } |
| 118 | + ], |
| 119 | + as: 'airlines' |
| 120 | + } |
| 121 | + }, |
| 122 | + { |
| 123 | + $project: { |
| 124 | + _id: 0, |
| 125 | + name: 1, |
| 126 | + airlines: { |
| 127 | + $filter: { |
| 128 | + input: '$airlines', |
| 129 | + as: 'airline', |
| 130 | + cond: { $eq: ['$$airline.country', 'Canada'] } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + ]) |
| 136 | + .toArray(); |
| 137 | + // End aggregate example 4 |
| 138 | + } |
| 139 | + }); |
| 140 | +}); |
0 commit comments