|
19 | 19 | // counts each borough's matching documents |
20 | 20 | // start-array-match-group |
21 | 21 | $pipeline = [ |
22 | | - ['$match' => ['cuisine' => 'Bakery']], |
23 | | - ['$group' => ['_id' => '$borough', 'count' => ['$sum' => 1]]], |
| 22 | + ['$match' => ['cuisine' => 'Bakery']], |
| 23 | + ['$group' => ['_id' => '$borough', 'count' => ['$sum' => 1]]], |
24 | 24 | ]; |
25 | 25 |
|
26 | 26 | $cursor = $collection->aggregate($pipeline); |
27 | 27 |
|
28 | 28 | foreach ($cursor as $doc) { |
29 | | - echo json_encode($doc), PHP_EOL; |
| 29 | + echo json_encode($doc), PHP_EOL; |
30 | 30 | } |
31 | 31 | // end-array-match-group |
32 | 32 |
|
33 | 33 | // Performs the same aggregation operation as above but asks MongoDB to explain it |
34 | 34 | // start-array-explain |
35 | 35 | $pipeline = [ |
36 | | - ['$match' => ['cuisine' => 'Bakery']], |
37 | | - ['$group' => ['_id' => '$borough', 'count' => ['$sum' => 1]]], |
| 36 | + ['$match' => ['cuisine' => 'Bakery']], |
| 37 | + ['$group' => ['_id' => '$borough', 'count' => ['$sum' => 1]]], |
38 | 38 | ]; |
39 | 39 |
|
40 | 40 | $aggregate = new MongoDB\Operation\Aggregate( |
41 | | - $collection->getDatabaseName(), |
42 | | - $collection->getCollectionName(), |
43 | | - $pipeline |
| 41 | + $collection->getDatabaseName(), |
| 42 | + $collection->getCollectionName(), |
| 43 | + $pipeline |
44 | 44 | ); |
45 | 45 |
|
46 | 46 | $result = $collection->explain($aggregate); |
|
49 | 49 |
|
50 | 50 | // start-builder-match-group |
51 | 51 | $pipeline = new Pipeline( |
52 | | - Stage::match( |
53 | | - date: [ |
54 | | - Query::gte(new UTCDateTime(new DateTimeImmutable('2014-01-01'))), |
55 | | - Query::lt(new UTCDateTime(new DateTimeImmutable('2015-01-01'))), |
56 | | - ], |
57 | | - ), |
58 | | - Stage::group( |
59 | | - _id: Expression::dateToString(Expression::dateFieldPath('date'), '%Y-%m-%d'), |
60 | | - totalSaleAmount: Accumulator::sum( |
61 | | - Expression::multiply( |
62 | | - Expression::numberFieldPath('price'), |
63 | | - Expression::numberFieldPath('quantity'), |
64 | | - ), |
65 | | - ), |
66 | | - averageQuantity: Accumulator::avg( |
67 | | - Expression::numberFieldPath('quantity'), |
68 | | - ), |
69 | | - count: Accumulator::sum(1), |
70 | | - ), |
71 | | - Stage::sort( |
72 | | - totalSaleAmount: Sort::Desc, |
73 | | - ), |
| 52 | + Stage::match( |
| 53 | + date: [ |
| 54 | + Query::gte(new UTCDateTime(new DateTimeImmutable('2014-01-01'))), |
| 55 | + Query::lt(new UTCDateTime(new DateTimeImmutable('2015-01-01'))), |
| 56 | + ], |
| 57 | + ), |
| 58 | + Stage::group( |
| 59 | + _id: Expression::dateToString(Expression::dateFieldPath('date'), '%Y-%m-%d'), |
| 60 | + totalSaleAmount: Accumulator::sum( |
| 61 | + Expression::multiply( |
| 62 | + Expression::numberFieldPath('price'), |
| 63 | + Expression::numberFieldPath('quantity'), |
| 64 | + ), |
| 65 | + ), |
| 66 | + averageQuantity: Accumulator::avg( |
| 67 | + Expression::numberFieldPath('quantity'), |
| 68 | + ), |
| 69 | + count: Accumulator::sum(1), |
| 70 | + ), |
| 71 | + Stage::sort( |
| 72 | + totalSaleAmount: Sort::Desc, |
| 73 | + ), |
74 | 74 | ); |
75 | 75 |
|
76 | 76 | $cursor = $collection->aggregate(iterator_to_array($pipeline)); |
77 | 77 |
|
78 | 78 | foreach ($cursor as $doc) { |
79 | | - echo json_encode($doc), PHP_EOL; |
| 79 | + echo json_encode($doc), PHP_EOL; |
80 | 80 | } |
81 | 81 | // end-builder-match-group |
82 | 82 |
|
83 | 83 | // start-builder-unwind |
84 | 84 | $pipeline = new Pipeline( |
85 | | - Stage::unwind(Expression::arrayFieldPath('items')), |
86 | | - Stage::unwind(Expression::arrayFieldPath('items.tags')), |
87 | | - Stage::group( |
88 | | - _id: Expression::fieldPath('items.tags'), |
89 | | - totalSalesAmount: Accumulator::sum( |
90 | | - Expression::multiply( |
91 | | - Expression::numberFieldPath('items.price'), |
92 | | - Expression::numberFieldPath('items.quantity'), |
93 | | - ), |
94 | | - ), |
95 | | - ), |
| 85 | + Stage::unwind(Expression::arrayFieldPath('items')), |
| 86 | + Stage::unwind(Expression::arrayFieldPath('items.tags')), |
| 87 | + Stage::group( |
| 88 | + _id: Expression::fieldPath('items.tags'), |
| 89 | + totalSalesAmount: Accumulator::sum( |
| 90 | + Expression::multiply( |
| 91 | + Expression::numberFieldPath('items.price'), |
| 92 | + Expression::numberFieldPath('items.quantity'), |
| 93 | + ), |
| 94 | + ), |
| 95 | + ), |
96 | 96 | ); |
97 | 97 |
|
98 | 98 | $cursor = $collection->aggregate(iterator_to_array($pipeline)); |
99 | 99 |
|
100 | 100 | foreach ($cursor as $doc) { |
101 | | - echo json_encode($doc), PHP_EOL; |
| 101 | + echo json_encode($doc), PHP_EOL; |
102 | 102 | } |
103 | 103 | // end-builder-unwind |
104 | 104 |
|
105 | 105 | // start-builder-lookup |
106 | 106 | $pipeline = new Pipeline( |
107 | | - Stage::lookup( |
108 | | - from: 'inventory', |
109 | | - localField: 'item', |
110 | | - foreignField: 'sku', |
111 | | - as: 'inventory_docs', |
112 | | - ), |
| 107 | + Stage::lookup( |
| 108 | + from: 'inventory', |
| 109 | + localField: 'item', |
| 110 | + foreignField: 'sku', |
| 111 | + as: 'inventory_docs', |
| 112 | + ), |
113 | 113 | ); |
114 | 114 |
|
115 | 115 | /* Perform the aggregation on the orders collection */ |
116 | 116 | $cursor = $collection->aggregate(iterator_to_array($pipeline)); |
117 | 117 |
|
118 | 118 | foreach ($cursor as $doc) { |
119 | | - echo json_encode($doc), PHP_EOL; |
| 119 | + echo json_encode($doc), PHP_EOL; |
120 | 120 | } |
121 | 121 | // end-builder-lookup |
0 commit comments