|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| -use MongoDB\BSON\UTCDateTime; |
4 |
| -use MongoDB\Builder\Accumulator; |
5 |
| -use MongoDB\Builder\Expression; |
6 |
| -use MongoDB\Builder\Query; |
7 |
| -use MongoDB\Builder\Stage; |
8 |
| -use MongoDB\Builder\Type\Sort; |
9 |
| - |
10 | 3 | require 'vendor/autoload.php';
|
11 | 4 |
|
12 | 5 | $uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
|
|
48 | 41 |
|
49 | 42 | // start-builder-match-group
|
50 | 43 | $pipeline = [
|
51 |
| - Stage::match( |
| 44 | + MongoDB\Builder\Stage::match( |
52 | 45 | date: [
|
53 |
| - Query::gte(new UTCDateTime(new DateTimeImmutable('2014-01-01'))), |
54 |
| - Query::lt(new UTCDateTime(new DateTimeImmutable('2015-01-01'))), |
| 46 | + MongoDB\Builder\Query::gte(new MongoDB\BSON\UTCDateTime(new DateTimeImmutable('2014-01-01'))), |
| 47 | + MongoDB\Builder\Query::lt(new MongoDB\BSON\UTCDateTime(new DateTimeImmutable('2015-01-01'))), |
55 | 48 | ],
|
56 | 49 | ),
|
57 |
| - Stage::group( |
58 |
| - _id: Expression::dateToString(Expression::dateFieldPath('date'), '%Y-%m-%d'), |
59 |
| - totalSaleAmount: Accumulator::sum( |
60 |
| - Expression::multiply( |
61 |
| - Expression::numberFieldPath('price'), |
62 |
| - Expression::numberFieldPath('quantity'), |
| 50 | + MongoDB\Builder\Stage::group( |
| 51 | + _id: MongoDB\Builder\Expression::dateToString(MongoDB\Builder\Expression::dateFieldPath('date'), '%Y-%m-%d'), |
| 52 | + totalSaleAmount: MongoDB\Builder\Accumulator::sum( |
| 53 | + MongoDB\Builder\Expression::multiply( |
| 54 | + MongoDB\Builder\Expression::numberFieldPath('price'), |
| 55 | + MongoDB\Builder\Expression::numberFieldPath('quantity'), |
63 | 56 | ),
|
64 | 57 | ),
|
65 |
| - averageQuantity: Accumulator::avg( |
66 |
| - Expression::numberFieldPath('quantity'), |
| 58 | + averageQuantity: MongoDB\Builder\Accumulator::avg( |
| 59 | + MongoDB\Builder\Expression::numberFieldPath('quantity'), |
67 | 60 | ),
|
68 |
| - count: Accumulator::sum(1), |
| 61 | + count: MongoDB\Builder\Accumulator::sum(1), |
69 | 62 | ),
|
70 |
| - Stage::sort( |
71 |
| - totalSaleAmount: Sort::Desc, |
| 63 | + MongoDB\Builder\Stage::sort( |
| 64 | + totalSaleAmount: MongoDB\Builder\Type\Sort::Desc, |
72 | 65 | ),
|
73 | 66 | ];
|
74 | 67 |
|
|
81 | 74 |
|
82 | 75 | // start-builder-unwind
|
83 | 76 | $pipeline = [
|
84 |
| - Stage::unwind(Expression::arrayFieldPath('items')), |
85 |
| - Stage::unwind(Expression::arrayFieldPath('items.tags')), |
86 |
| - Stage::group( |
87 |
| - _id: Expression::fieldPath('items.tags'), |
88 |
| - totalSalesAmount: Accumulator::sum( |
89 |
| - Expression::multiply( |
90 |
| - Expression::numberFieldPath('items.price'), |
91 |
| - Expression::numberFieldPath('items.quantity'), |
| 77 | + MongoDB\Builder\Stage::unwind(MongoDB\Builder\Expression::arrayFieldPath('items')), |
| 78 | + MongoDB\Builder\Stage::unwind(MongoDB\Builder\Expression::arrayFieldPath('items.tags')), |
| 79 | + MongoDB\Builder\Stage::group( |
| 80 | + _id: MongoDB\Builder\Expression::fieldPath('items.tags'), |
| 81 | + totalSalesAmount: MongoDB\Builder\Accumulator::sum( |
| 82 | + MongoDB\Builder\Expression::multiply( |
| 83 | + MongoDB\Builder\Expression::numberFieldPath('items.price'), |
| 84 | + MongoDB\Builder\Expression::numberFieldPath('items.quantity'), |
92 | 85 | ),
|
93 | 86 | ),
|
94 | 87 | ),
|
|
101 | 94 | }
|
102 | 95 | // end-builder-unwind
|
103 | 96 |
|
| 97 | +$collection = $client->db->orders; |
| 98 | + |
104 | 99 | // start-builder-lookup
|
105 | 100 | $pipeline = [
|
106 |
| - Stage::lookup( |
| 101 | + MongoDB\Builder\Stage::lookup( |
107 | 102 | from: 'inventory',
|
108 | 103 | localField: 'item',
|
109 | 104 | foreignField: 'sku',
|
|
0 commit comments