|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MongoDB\Tests\AggregationBlogPost; |
| 4 | + |
| 5 | +use MongoDB\BSON\Document; |
| 6 | +use MongoDB\BSON\Serializable; |
| 7 | +use MongoDB\Builder\Accumulator; |
| 8 | +use MongoDB\Builder\Expression; |
| 9 | +use MongoDB\Builder\Pipeline; |
| 10 | +use MongoDB\Builder\Query; |
| 11 | +use MongoDB\Builder\Stage; |
| 12 | +use MongoDB\Builder\Type\AccumulatorInterface; |
| 13 | +use MongoDB\Tests\TestCase; |
| 14 | +use stdClass; |
| 15 | +use function MongoDB\object; |
| 16 | + |
| 17 | +class Example4Test extends TestCase |
| 18 | +{ |
| 19 | + public function testFirstPipeline(): void |
| 20 | + { |
| 21 | + $pipeline = new Pipeline( |
| 22 | + Stage::lookup( |
| 23 | + as: 'movie', |
| 24 | + from: 'movies', |
| 25 | + localField: 'movie_id', |
| 26 | + foreignField: '_id', |
| 27 | + ), |
| 28 | + Stage::unwind(Expression::arrayFieldPath('movie')), |
| 29 | + Stage::replaceRoot(object( |
| 30 | + comment_id: Expression::objectIdFieldPath('_id'), |
| 31 | + user: Expression::stringFieldPath('name'), |
| 32 | + movie_id: Expression::objectIdFieldPath('movie_id'), |
| 33 | + movie_title: Expression::stringFieldPath('movie.title'), |
| 34 | + genres: Expression::arrayFieldPath('movie.genres'), |
| 35 | + rating: Expression::intFieldPath('movie.imdb.rating'), |
| 36 | + comment: Expression::stringFieldPath('text'), |
| 37 | + )), |
| 38 | + Stage::unwind(Expression::arrayFieldPath('genres')), |
| 39 | + Stage::group( |
| 40 | + _id: object( |
| 41 | + user: Expression::stringFieldPath('user'), |
| 42 | + genre: Expression::stringFieldPath('genres'), |
| 43 | + ), |
| 44 | + totalComments: Accumulator::sum(1), |
| 45 | + averageRating: Accumulator::avg(Expression::intFieldPath('rating')), |
| 46 | + ), |
| 47 | + Stage::group( |
| 48 | + _id: Expression::stringFieldPath('_id.user'), |
| 49 | + preferences: Accumulator::push(object( |
| 50 | + genre: Expression::stringFieldPath('_id.genre'), |
| 51 | + totalComments: Expression::intFieldPath('totalComments'), |
| 52 | + averageRating: Expression::round(Expression::avg(Expression::doubleFieldPath('averageRating')), 2), |
| 53 | + )), |
| 54 | + totalComments: Accumulator::sum(Expression::intFieldPath('totalComments')), |
| 55 | + ), |
| 56 | + Stage::sort(totalComments: -1), |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + public function testExtractLookupStage(): void |
| 61 | + { |
| 62 | + $pipeline = new Pipeline( |
| 63 | + $this->lookupMovie(), |
| 64 | + Stage::unwind(Expression::arrayFieldPath('movie')), |
| 65 | + Stage::replaceRoot(object( |
| 66 | + comment_id: Expression::objectIdFieldPath('_id'), |
| 67 | + user: Expression::stringFieldPath('name'), |
| 68 | + movie_id: Expression::objectIdFieldPath('movie_id'), |
| 69 | + movie_title: Expression::stringFieldPath('movie.title'), |
| 70 | + genres: Expression::arrayFieldPath('movie.genres'), |
| 71 | + rating: Expression::intFieldPath('movie.imdb.rating'), |
| 72 | + comment: Expression::stringFieldPath('text'), |
| 73 | + )), |
| 74 | + Stage::unwind(Expression::arrayFieldPath('genres')), |
| 75 | + Stage::group( |
| 76 | + _id: object( |
| 77 | + user: Expression::stringFieldPath('user'), |
| 78 | + genre: Expression::stringFieldPath('genres'), |
| 79 | + ), |
| 80 | + totalComments: Accumulator::sum(1), |
| 81 | + averageRating: Accumulator::avg(Expression::intFieldPath('rating')), |
| 82 | + ), |
| 83 | + Stage::group( |
| 84 | + _id: Expression::stringFieldPath('_id.user'), |
| 85 | + preferences: Accumulator::push(object( |
| 86 | + genre: Expression::stringFieldPath('_id.genre'), |
| 87 | + totalComments: Expression::intFieldPath('totalComments'), |
| 88 | + averageRating: Expression::round(Expression::avg(Expression::doubleFieldPath('averageRating')), 2), |
| 89 | + )), |
| 90 | + totalComments: Accumulator::sum(Expression::intFieldPath('totalComments')), |
| 91 | + ), |
| 92 | + Stage::sort(totalComments: -1), |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + public function testExtractLookupSingleMovie(): void |
| 97 | + { |
| 98 | + $pipeline = new Pipeline( |
| 99 | + $this->lookupSingleMovie(), |
| 100 | + Stage::replaceRoot(object( |
| 101 | + comment_id: Expression::objectIdFieldPath('_id'), |
| 102 | + user: Expression::stringFieldPath('name'), |
| 103 | + movie_id: Expression::objectIdFieldPath('movie_id'), |
| 104 | + movie_title: Expression::stringFieldPath('movie.title'), |
| 105 | + genres: Expression::arrayFieldPath('movie.genres'), |
| 106 | + rating: Expression::intFieldPath('movie.imdb.rating'), |
| 107 | + comment: Expression::stringFieldPath('text'), |
| 108 | + )), |
| 109 | + Stage::unwind(Expression::arrayFieldPath('genres')), |
| 110 | + Stage::group( |
| 111 | + _id: object( |
| 112 | + user: Expression::stringFieldPath('user'), |
| 113 | + genre: Expression::stringFieldPath('genres'), |
| 114 | + ), |
| 115 | + totalComments: Accumulator::sum(1), |
| 116 | + averageRating: Accumulator::avg(Expression::intFieldPath('rating')), |
| 117 | + ), |
| 118 | + Stage::group( |
| 119 | + _id: Expression::stringFieldPath('_id.user'), |
| 120 | + preferences: Accumulator::push(object( |
| 121 | + genre: Expression::stringFieldPath('_id.genre'), |
| 122 | + totalComments: Expression::intFieldPath('totalComments'), |
| 123 | + averageRating: Expression::round(Expression::avg(Expression::doubleFieldPath('averageRating')), 2), |
| 124 | + )), |
| 125 | + totalComments: Accumulator::sum(Expression::intFieldPath('totalComments')), |
| 126 | + ), |
| 127 | + Stage::sort(totalComments: -1), |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + public function testExtractGroupByUserAndGenre(): void |
| 132 | + { |
| 133 | + $pipeline = new Pipeline( |
| 134 | + $this->lookupSingleMovie(), |
| 135 | + Stage::replaceRoot(object( |
| 136 | + comment_id: Expression::objectIdFieldPath('_id'), |
| 137 | + user: Expression::stringFieldPath('name'), |
| 138 | + movie_id: Expression::objectIdFieldPath('movie_id'), |
| 139 | + movie_title: Expression::stringFieldPath('movie.title'), |
| 140 | + genres: Expression::arrayFieldPath('movie.genres'), |
| 141 | + rating: Expression::intFieldPath('movie.imdb.rating'), |
| 142 | + comment: Expression::stringFieldPath('text'), |
| 143 | + )), |
| 144 | + Stage::unwind(Expression::arrayFieldPath('genres')), |
| 145 | + $this->groupByUserAndGenre( |
| 146 | + totalComments: Accumulator::sum(1), |
| 147 | + averageRating: Accumulator::avg(Expression::intFieldPath('rating')), |
| 148 | + ), |
| 149 | + Stage::group( |
| 150 | + _id: Expression::stringFieldPath('_id.user'), |
| 151 | + preferences: Accumulator::push(object( |
| 152 | + genre: Expression::stringFieldPath('_id.genre'), |
| 153 | + totalComments: Expression::intFieldPath('totalComments'), |
| 154 | + averageRating: Expression::round(Expression::avg(Expression::doubleFieldPath('averageRating')), 2), |
| 155 | + )), |
| 156 | + totalComments: Accumulator::sum(Expression::intFieldPath('totalComments')), |
| 157 | + ), |
| 158 | + Stage::sort(totalComments: -1), |
| 159 | + ); |
| 160 | + } |
| 161 | + |
| 162 | + private function lookupMovie(): Stage\LookupStage |
| 163 | + { |
| 164 | + return Stage::lookup( |
| 165 | + as: 'movie', |
| 166 | + from: 'movies', |
| 167 | + localField: 'movie_id', |
| 168 | + foreignField: '_id', |
| 169 | + ); |
| 170 | + } |
| 171 | + |
| 172 | + private function lookupSingleMovie(): Pipeline |
| 173 | + { |
| 174 | + return new Pipeline( |
| 175 | + Stage::lookup( |
| 176 | + as: 'movie', |
| 177 | + from: 'movies', |
| 178 | + localField: 'movie_id', |
| 179 | + foreignField: '_id', |
| 180 | + ), |
| 181 | + Stage::unwind(Expression::arrayFieldPath('movie')), |
| 182 | + ); |
| 183 | + } |
| 184 | + |
| 185 | + private function groupByUserAndGenre(Document|Serializable|AccumulatorInterface|stdClass|array ...$field): Stage\GroupStage |
| 186 | + { |
| 187 | + return Stage::group( |
| 188 | + ...$field, |
| 189 | + _id: object( |
| 190 | + user: Expression::stringFieldPath('user'), |
| 191 | + genre: Expression::stringFieldPath('genres'), |
| 192 | + ), |
| 193 | + ); |
| 194 | + } |
| 195 | +} |
0 commit comments