Skip to content

Commit 967767e

Browse files
committed
JM feedback
1 parent 6132ff6 commit 967767e

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

source/aggregation.txt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ Aggregation Example
9494
free MongoDB Atlas cluster and load the sample datasets, see the :atlas:`Get Started with Atlas
9595
</getting-started>` guide.
9696

97-
To perform an aggregation, pass an array containing the aggregation pipeline
98-
stages to the ``MongoDB\Collection::aggregate()`` method.
97+
To perform an aggregation, pass an array containing the pipeline stages to
98+
the ``MongoDB\Collection::aggregate()`` method.
9999

100100
The following code example produces a count of the number of bakeries in each borough
101101
of New York. To do so, it uses an aggregation pipeline that contains the following stages:
@@ -136,13 +136,12 @@ An execution plan is a potential way in which MongoDB can complete an operation.
136136
When you instruct MongoDB to explain an operation, it returns both the
137137
plan MongoDB executed and any rejected execution plans.
138138

139-
To explain an aggregation operation, run the ``explain`` database command by passing
140-
the command information to the ``MongoDB\Database::command()`` method. You must specify the
141-
``aggregate``, ``pipeline``, and ``cursor`` fields in the ``explain`` command document
142-
to explain the aggregation.
139+
To explain an aggregation operation, construct a ``MongoDB\Operation\Aggregate`` object
140+
and pass the database, collection, and pipeline stages as parameters. Then, pass the
141+
``MongoDB\Operation\Aggregate`` object to the ``MongoDB\Collection::explain()`` method.
143142

144-
The following example instructs MongoDB to explain the aggregation operation from the
145-
preceding :ref:`php-aggregation-example`:
143+
The following example instructs MongoDB to explain the aggregation operation
144+
from the preceding :ref:`php-aggregation-example`:
146145

147146
.. io-code-block::
148147
:copyable:
@@ -162,7 +161,6 @@ preceding :ref:`php-aggregation-example`:
162161
"maxIndexedAndSolutionsReached":false,"maxScansToExplodeReached":false,"winningPlan":{
163162
... }
164163

165-
166164
Additional Information
167165
----------------------
168166

source/includes/aggregation.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
55
$client = new MongoDB\Client($uri);
66

7-
$db = $client->sample_restaurants;
8-
$collection = $db->restaurants;
7+
$collection = $client->sample_restaurants->restaurants;
98

109
// Retrieves documents with a cuisine value of "Bakery", groups them by "borough", and
1110
// counts each borough's matching documents
1211
// start-match-group
1312
$pipeline = [
1413
['$match' => ['cuisine' => 'Bakery']],
15-
['$group' => ['_id' => '$borough', 'count' => ['$sum' => 1]]]
14+
['$group' => ['_id' => '$borough', 'count' => ['$sum' => 1]]],
1615
];
1716

1817
$cursor = $collection->aggregate($pipeline);
@@ -26,18 +25,16 @@
2625
// start-explain
2726
$pipeline = [
2827
['$match' => ['cuisine' => 'Bakery']],
29-
['$group' => ['_id' => '$borough', 'count' => ['$sum' => 1]]]
28+
['$group' => ['_id' => '$borough', 'count' => ['$sum' => 1]]],
3029
];
3130

32-
$command = [
33-
'explain' => [
34-
'aggregate' => 'restaurants',
35-
'pipeline' => $pipeline,
36-
'cursor' => new stdClass()
37-
]
38-
];
31+
$aggregate = new MongoDB\Operation\Aggregate(
32+
$collection->getDatabaseName(),
33+
$collection->getCollectionName(),
34+
$pipeline
35+
);
3936

40-
$result = $db->command($command)->toArray();
41-
echo json_encode($result[0]) . PHP_EOL;
37+
$result = $collection->explain($aggregate);
38+
echo json_encode($result) . PHP_EOL;
4239
// end-explain
4340

0 commit comments

Comments
 (0)