Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Agg builder blog post #2

Draft
wants to merge 5 commits into
base: v2.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions tests/AggregationBlogPost/Example1Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace MongoDB\Tests\AggregationBlogPost;

use MongoDB\Builder\Accumulator;
use MongoDB\Builder\Expression;
use MongoDB\Builder\Pipeline;
use MongoDB\Builder\Stage;
use MongoDB\Tests\TestCase;

class Example1Test extends TestCase
{
public function testExample1(): void
{
$pipeline = new Pipeline(
Stage::group(
_id: Expression::stringFieldPath('email'),
totalComments: Accumulator::sum(1),
),
Stage::sort(totalComments: -1),
Stage::limit(5),
);
}
}
38 changes: 38 additions & 0 deletions tests/AggregationBlogPost/Example2Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace MongoDB\Tests\AggregationBlogPost;

use MongoDB\Builder\Accumulator;
use MongoDB\Builder\Expression;
use MongoDB\Builder\Pipeline;
use MongoDB\Builder\Query;
use MongoDB\Builder\Stage;
use MongoDB\Tests\TestCase;
use function MongoDB\object;

Check failure on line 11 in tests/AggregationBlogPost/Example2Test.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected 1 line between different types of use statement, found 0.

class Example2Test extends TestCase
{
public function testExample2(): void
{
$pipeline = new Pipeline(
Stage::match(...[
'imdb.rating' => Query::exists(),
]),
Stage::addFields(
month: Expression::month(Expression::dateFieldPath('released')),
year: Expression::year(Expression::dateFieldPath('released')),
),
Stage::group(
_id: object(
year: Expression::intFieldPath('year'),
month: Expression::intFieldPath('month'),
),
averageRating: Accumulator::avg(Expression::intFieldPath('imdb.rating')),
),
Stage::sort(...[
'_id.year' => 1,
'_id.month' => 1,
]),
);
}
}
46 changes: 46 additions & 0 deletions tests/AggregationBlogPost/Example3Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace MongoDB\Tests\AggregationBlogPost;

use MongoDB\Builder\Accumulator;
use MongoDB\Builder\Expression;
use MongoDB\Builder\Pipeline;
use MongoDB\Builder\Query;

Check failure on line 8 in tests/AggregationBlogPost/Example3Test.php

View workflow job for this annotation

GitHub Actions / phpcs

Type MongoDB\Builder\Query is not used in this file.
use MongoDB\Builder\Stage;
use MongoDB\Tests\TestCase;
use function MongoDB\object;

Check failure on line 11 in tests/AggregationBlogPost/Example3Test.php

View workflow job for this annotation

GitHub Actions / phpcs

Type MongoDB\object is not used in this file.

Check failure on line 11 in tests/AggregationBlogPost/Example3Test.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected 1 line between different types of use statement, found 0.

class Example3Test extends TestCase
{
public function testExample3(): void
{
$pipeline = new Pipeline(
Stage::lookup(
as: 'comments',
from: 'comments',
localField: '_id',
foreignField: 'movie_id',
),
Stage::facet(
mostCommentedMovies: new Pipeline(
Stage::project(
_id: 0,
title: 1,
commentCount: Expression::size(Expression::arrayFieldPath('comments')),
),
Stage::sort(commentCount: -1),
Stage::limit(5),
),
mostCommentedGenre: new Pipeline(
Stage::unwind(Expression::arrayFieldPath('genres')),
Stage::group(
_id: expression::stringFieldPath('genres'),
totalComments: Accumulator::sum(Expression::size(Expression::arrayFieldPath('comments'))),
),
Stage::sort(totalComments: -1),
Stage::limit(5),
),
),
);
}
}
195 changes: 195 additions & 0 deletions tests/AggregationBlogPost/Example4Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
<?php

namespace MongoDB\Tests\AggregationBlogPost;

use MongoDB\BSON\Document;
use MongoDB\BSON\Serializable;
use MongoDB\Builder\Accumulator;
use MongoDB\Builder\Expression;
use MongoDB\Builder\Pipeline;
use MongoDB\Builder\Query;
use MongoDB\Builder\Stage;
use MongoDB\Builder\Type\AccumulatorInterface;
use MongoDB\Tests\TestCase;
use stdClass;
use function MongoDB\object;

class Example4Test extends TestCase
{
public function testFirstPipeline(): void
{
$pipeline = new Pipeline(
Stage::lookup(
as: 'movie',
from: 'movies',
localField: 'movie_id',
foreignField: '_id',
),
Stage::unwind(Expression::arrayFieldPath('movie')),
Stage::replaceRoot(object(
comment_id: Expression::objectIdFieldPath('_id'),
user: Expression::stringFieldPath('name'),
movie_id: Expression::objectIdFieldPath('movie_id'),
movie_title: Expression::stringFieldPath('movie.title'),
genres: Expression::arrayFieldPath('movie.genres'),
rating: Expression::intFieldPath('movie.imdb.rating'),
comment: Expression::stringFieldPath('text'),
)),
Stage::unwind(Expression::arrayFieldPath('genres')),
Stage::group(
_id: object(
user: Expression::stringFieldPath('user'),
genre: Expression::stringFieldPath('genres'),
),
totalComments: Accumulator::sum(1),
averageRating: Accumulator::avg(Expression::intFieldPath('rating')),
),
Stage::group(
_id: Expression::stringFieldPath('_id.user'),
preferences: Accumulator::push(object(
genre: Expression::stringFieldPath('_id.genre'),
totalComments: Expression::intFieldPath('totalComments'),
averageRating: Expression::round(Expression::avg(Expression::doubleFieldPath('averageRating')), 2),
)),
totalComments: Accumulator::sum(Expression::intFieldPath('totalComments')),
),
Stage::sort(totalComments: -1),
);
}

public function testExtractLookupStage(): void
{
$pipeline = new Pipeline(
$this->lookupMovie(),
Stage::unwind(Expression::arrayFieldPath('movie')),
Stage::replaceRoot(object(
comment_id: Expression::objectIdFieldPath('_id'),
user: Expression::stringFieldPath('name'),
movie_id: Expression::objectIdFieldPath('movie_id'),
movie_title: Expression::stringFieldPath('movie.title'),
genres: Expression::arrayFieldPath('movie.genres'),
rating: Expression::intFieldPath('movie.imdb.rating'),
comment: Expression::stringFieldPath('text'),
)),
Stage::unwind(Expression::arrayFieldPath('genres')),
Stage::group(
_id: object(
user: Expression::stringFieldPath('user'),
genre: Expression::stringFieldPath('genres'),
),
totalComments: Accumulator::sum(1),
averageRating: Accumulator::avg(Expression::intFieldPath('rating')),
),
Stage::group(
_id: Expression::stringFieldPath('_id.user'),
preferences: Accumulator::push(object(
genre: Expression::stringFieldPath('_id.genre'),
totalComments: Expression::intFieldPath('totalComments'),
averageRating: Expression::round(Expression::avg(Expression::doubleFieldPath('averageRating')), 2),
)),
totalComments: Accumulator::sum(Expression::intFieldPath('totalComments')),
),
Stage::sort(totalComments: -1),
);
}

public function testExtractLookupSingleMovie(): void
{
$pipeline = new Pipeline(
$this->lookupSingleMovie(),
Stage::replaceRoot(object(
comment_id: Expression::objectIdFieldPath('_id'),
user: Expression::stringFieldPath('name'),
movie_id: Expression::objectIdFieldPath('movie_id'),
movie_title: Expression::stringFieldPath('movie.title'),
genres: Expression::arrayFieldPath('movie.genres'),
rating: Expression::intFieldPath('movie.imdb.rating'),
comment: Expression::stringFieldPath('text'),
)),
Stage::unwind(Expression::arrayFieldPath('genres')),
Stage::group(
_id: object(
user: Expression::stringFieldPath('user'),
genre: Expression::stringFieldPath('genres'),
),
totalComments: Accumulator::sum(1),
averageRating: Accumulator::avg(Expression::intFieldPath('rating')),
),
Stage::group(
_id: Expression::stringFieldPath('_id.user'),
preferences: Accumulator::push(object(
genre: Expression::stringFieldPath('_id.genre'),
totalComments: Expression::intFieldPath('totalComments'),
averageRating: Expression::round(Expression::avg(Expression::doubleFieldPath('averageRating')), 2),
)),
totalComments: Accumulator::sum(Expression::intFieldPath('totalComments')),
),
Stage::sort(totalComments: -1),
);
}

public function testExtractGroupByUserAndGenre(): void
{
$pipeline = new Pipeline(
$this->lookupSingleMovie(),
Stage::replaceRoot(object(
comment_id: Expression::objectIdFieldPath('_id'),
user: Expression::stringFieldPath('name'),
movie_id: Expression::objectIdFieldPath('movie_id'),
movie_title: Expression::stringFieldPath('movie.title'),
genres: Expression::arrayFieldPath('movie.genres'),
rating: Expression::intFieldPath('movie.imdb.rating'),
comment: Expression::stringFieldPath('text'),
)),
Stage::unwind(Expression::arrayFieldPath('genres')),
$this->groupByUserAndGenre(
totalComments: Accumulator::sum(1),
averageRating: Accumulator::avg(Expression::intFieldPath('rating')),
),
Stage::group(
_id: Expression::stringFieldPath('_id.user'),
preferences: Accumulator::push(object(
genre: Expression::stringFieldPath('_id.genre'),
totalComments: Expression::intFieldPath('totalComments'),
averageRating: Expression::round(Expression::avg(Expression::doubleFieldPath('averageRating')), 2),
)),
totalComments: Accumulator::sum(Expression::intFieldPath('totalComments')),
),
Stage::sort(totalComments: -1),
);
}

private function lookupMovie(): Stage\LookupStage
{
return Stage::lookup(
as: 'movie',
from: 'movies',
localField: 'movie_id',
foreignField: '_id',
);
}

private function lookupSingleMovie(): Pipeline
{
return new Pipeline(
Stage::lookup(
as: 'movie',
from: 'movies',
localField: 'movie_id',
foreignField: '_id',
),
Stage::unwind(Expression::arrayFieldPath('movie')),
);
}

private function groupByUserAndGenre(Document|Serializable|AccumulatorInterface|stdClass|array ...$field): Stage\GroupStage
{
return Stage::group(
...$field,
_id: object(
user: Expression::stringFieldPath('user'),
genre: Expression::stringFieldPath('genres'),
),
);
}
}
Loading