From dd3aaa4ed17b25b7a5362a1ac8c0c4417a9d1b0c Mon Sep 17 00:00:00 2001 From: norareidy Date: Wed, 29 Jan 2025 16:42:17 -0500 Subject: [PATCH 1/4] DOCSP-46415: Group by aggregation methods --- .../query-builder/QueryBuilderTest.php | 24 +++++++++ docs/query-builder.txt | 50 +++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/docs/includes/query-builder/QueryBuilderTest.php b/docs/includes/query-builder/QueryBuilderTest.php index d99796fb2..8d2207e03 100644 --- a/docs/includes/query-builder/QueryBuilderTest.php +++ b/docs/includes/query-builder/QueryBuilderTest.php @@ -281,6 +281,30 @@ public function testAggWithFilter(): void $this->assertIsFloat($result); } + public function testCountByGroup(): void + { + // begin count by group + $result = DB::table('movies') + ->groupBy('imdb.rating') + ->orderBy('imdb.rating') + ->countByGroup(); + // end count by group + + $this->assertIsArray($result); + } + + public function testSumByGroup(): void + { + // begin sum by group + $result = DB::table('movies') + ->groupBy('year') + ->orderBy('year') + ->sumByGroup('awards.wins'); + // end sum by group + + $this->assertIsArray($result); + } + public function testOrderBy(): void { // begin query orderBy diff --git a/docs/query-builder.txt b/docs/query-builder.txt index b3c89b0ae..f8e20c7f3 100644 --- a/docs/query-builder.txt +++ b/docs/query-builder.txt @@ -658,6 +658,56 @@ value of ``imdb.rating`` of those matches by using the :start-after: begin aggregation with filter :end-before: end aggregation with filter +Aggregate By Group Examples +~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +You can perform an aggregation operation on a group +of documents by using the following query builder +methods, which return a single aggregated value for +each group: + +- ``countByGroup()``: Retrieves a count of the given field's + distinct values for each group + +- ``minByGroup()``: Retrieves the minimum value of the + given field for each group + +- ``maxByGroup()``: Retrieves the maximum value of the + given field for each group + +- ``sumByGroup()``: Retrieves the sum of a given field's + values for each group + +- ``avgByGroup()``: Retrieves the average of a given field's + values for each group + +countByGroup() Example +`````````````````````` + +The following example groups documents in the ``movies`` +collection by their ``imdb.rating`` values and returns +the number of documents that have each distinct +``imdb.rating`` value: + +.. literalinclude:: /includes/query-builder/QueryBuilderTest.php + :language: php + :dedent: + :start-after: begin count by group + :end-before: end count by group + +sumByGroup() Example +```````````````````` + +The following example groups documents in the ``movies`` +collection by their ``year`` values and returns +a sum of ``awards.wins`` values for each year: + +.. literalinclude:: /includes/query-builder/QueryBuilderTest.php + :language: php + :dedent: + :start-after: begin sum by group + :end-before: end sum by group + .. _laravel-options-query-builder: Set Query-Level Options From 130313299ffd285e762b959af59159af50124ced Mon Sep 17 00:00:00 2001 From: norareidy Date: Wed, 29 Jan 2025 17:04:24 -0500 Subject: [PATCH 2/4] edits --- docs/includes/query-builder/QueryBuilderTest.php | 1 + docs/query-builder.txt | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/includes/query-builder/QueryBuilderTest.php b/docs/includes/query-builder/QueryBuilderTest.php index 8d2207e03..3a6dab8e6 100644 --- a/docs/includes/query-builder/QueryBuilderTest.php +++ b/docs/includes/query-builder/QueryBuilderTest.php @@ -7,6 +7,7 @@ use Carbon\Carbon; use Illuminate\Database\Query\Builder; use Illuminate\Pagination\AbstractPaginator; +use Illuminate\Support\Collection as LaravelCollection; use Illuminate\Support\Facades\DB; use MongoDB\BSON\ObjectId; use MongoDB\BSON\Regex; diff --git a/docs/query-builder.txt b/docs/query-builder.txt index f8e20c7f3..23311831a 100644 --- a/docs/query-builder.txt +++ b/docs/query-builder.txt @@ -682,7 +682,7 @@ each group: values for each group countByGroup() Example -`````````````````````` +^^^^^^^^^^^^^^^^^^^^^^ The following example groups documents in the ``movies`` collection by their ``imdb.rating`` values and returns @@ -696,7 +696,7 @@ the number of documents that have each distinct :end-before: end count by group sumByGroup() Example -```````````````````` +^^^^^^^^^^^^^^^^^^^^ The following example groups documents in the ``movies`` collection by their ``year`` values and returns From 7f74176e60f25c5783854f0358fe3b80b831ce27 Mon Sep 17 00:00:00 2001 From: norareidy <107268623+norareidy@users.noreply.github.com> Date: Wed, 29 Jan 2025 22:05:04 +0000 Subject: [PATCH 3/4] apply phpcbf formatting --- docs/includes/query-builder/QueryBuilderTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/includes/query-builder/QueryBuilderTest.php b/docs/includes/query-builder/QueryBuilderTest.php index 3a6dab8e6..8d2207e03 100644 --- a/docs/includes/query-builder/QueryBuilderTest.php +++ b/docs/includes/query-builder/QueryBuilderTest.php @@ -7,7 +7,6 @@ use Carbon\Carbon; use Illuminate\Database\Query\Builder; use Illuminate\Pagination\AbstractPaginator; -use Illuminate\Support\Collection as LaravelCollection; use Illuminate\Support\Facades\DB; use MongoDB\BSON\ObjectId; use MongoDB\BSON\Regex; From d667f19070e922d6bb297c86f3533cdfa647fac3 Mon Sep 17 00:00:00 2001 From: norareidy Date: Wed, 29 Jan 2025 17:18:09 -0500 Subject: [PATCH 4/4] code edit --- docs/includes/query-builder/QueryBuilderTest.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/includes/query-builder/QueryBuilderTest.php b/docs/includes/query-builder/QueryBuilderTest.php index 3a6dab8e6..f09f8db0e 100644 --- a/docs/includes/query-builder/QueryBuilderTest.php +++ b/docs/includes/query-builder/QueryBuilderTest.php @@ -7,7 +7,6 @@ use Carbon\Carbon; use Illuminate\Database\Query\Builder; use Illuminate\Pagination\AbstractPaginator; -use Illuminate\Support\Collection as LaravelCollection; use Illuminate\Support\Facades\DB; use MongoDB\BSON\ObjectId; use MongoDB\BSON\Regex; @@ -291,7 +290,7 @@ public function testCountByGroup(): void ->countByGroup(); // end count by group - $this->assertIsArray($result); + $this->assertInstanceOf(\Illuminate\Support\Collection::class, $result); } public function testSumByGroup(): void @@ -303,7 +302,7 @@ public function testSumByGroup(): void ->sumByGroup('awards.wins'); // end sum by group - $this->assertIsArray($result); + $this->assertInstanceOf(\Illuminate\Support\Collection::class, $result); } public function testOrderBy(): void