-
Notifications
You must be signed in to change notification settings - Fork 34
DOCSP-41988: Aggregation #115
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
DOCSP-41988: Aggregation #115
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work! LGTM w/ a couple suggestions!
source/aggregation.txt
Outdated
Limitations | ||
~~~~~~~~~~~ | ||
|
||
Keep the following limitations in mind when using aggregation operations: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
Keep the following limitations in mind when using aggregation operations: | |
Consider the following limitations when performing aggregation operations: |
source/aggregation.txt
Outdated
.. important:: $graphLookup Exception | ||
|
||
The :manual:`$graphLookup | ||
</reference/operator/aggregation/graphLookup/>` stage has a strict | ||
memory limit of 100 megabytes and ignores the ``allowDiskUse`` option. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this admonition should be indented since it applies to the second bullet above
source/aggregation.txt
Outdated
To view information about how MongoDB executes your operation, you can | ||
instruct the MongoDB query planner to **explain** it. When MongoDB explains | ||
an operation, it returns **execution plans** and performance statistics. | ||
An execution plan is a potential way MongoDB can complete an operation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An execution plan is a potential way MongoDB can complete an operation. | |
An execution plan is a potential way in which MongoDB can complete an operation. |
source/aggregation.txt
Outdated
the command information to the ``MongoDB\Database::command()`` method. You must set the | ||
``aggregate``, ``pipeline``, and ``cursor`` fields of the ``explain`` command document | ||
to explain the aggregation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
S: The word "set" here made me think you were going to explain what these fields needed to be set to
the command information to the ``MongoDB\Database::command()`` method. You must set the | |
``aggregate``, ``pipeline``, and ``cursor`` fields of the ``explain`` command document | |
to explain the aggregation. | |
the command information to the ``MongoDB\Database::command()`` method. You must specify the | |
``aggregate``, ``pipeline``, and ``cursor`` fields in the ``explain`` command document | |
to explain the aggregation. |
source/aggregation.txt
Outdated
To view a full list of expression operators, see :manual:`Aggregation | ||
Operators. </reference/operator/aggregation/>` | ||
|
||
To learn about assembling an aggregation pipeline and view examples, see |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To learn about assembling an aggregation pipeline and view examples, see | |
To learn about assembling an aggregation pipeline and to view examples, see |
source/aggregation.txt
Outdated
free MongoDB Atlas cluster and load the sample datasets, see the :atlas:`Get Started with Atlas | ||
</getting-started>` guide. | ||
|
||
To perform an aggregation, pass an array containing the aggregation pipeline |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could "aggregation pipeline stages" be simplified as "pipeline stages"? I think the reader already has the context and we could avoid word repetition.
source/aggregation.txt
Outdated
To explain an aggregation operation, run the ``explain`` database command by passing | ||
the command information to the ``MongoDB\Database::command()`` method. You must specify the | ||
``aggregate``, ``pipeline``, and ``cursor`` fields in the ``explain`` command document | ||
to explain the aggregation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: PHPLIB actually has an explain
command helper: MongoDB\Collection::explain()
.
I think it'd be preferable for the example to demonstrate that, and it'd also provide an opportunity to link to the API reference for that method (like you do for aggregate()
above).
Users will need to construct a MongoDB\Operation\Aggregate
object to pass to the explain()
method. An existing code example of that can be found in the Collection::aggregate()
helper. The benefit of this approach is that the pipeline and any objects will receive the same validation as the aggregate()
helper, and users won't need to think about the cursor
field.
source/includes/aggregation.php
Outdated
]; | ||
|
||
$result = $db->command($command)->toArray(); | ||
echo json_encode($result[0]) . PHP_EOL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In contrast to #113, I think you're fine to use json_encode()
here because the aggregation result documents will only include strings and integers. If that ever changes, you'd want to use PHPC's BSON API to output Extended JSON.
source/includes/aggregation.php
Outdated
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset'); | ||
$client = new MongoDB\Client($uri); | ||
|
||
$db = $client->sample_restaurants; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you end up using Collection::explain()
, there will be no need to assign a separate $db
variable.
source/includes/aggregation.php
Outdated
// start-match-group | ||
$pipeline = [ | ||
['$match' => ['cuisine' => 'Bakery']], | ||
['$group' => ['_id' => '$borough', 'count' => ['$sum' => 1]]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
['$group' => ['_id' => '$borough', 'count' => ['$sum' => 1]]] | |
['$group' => ['_id' => '$borough', 'count' => ['$sum' => 1]]], |
Note: it's typically good practice to use trailing commas after all array elements, even though it's not required for the final element. I believe we actually enforce this in the PHPLIB coding standard, as it leads to more concise diffs when changing arrays.
This can apply to the $pipeline
assignment below, too. I assume that is intentionally duplicated since it's displayed in a different location within the tutorial.
source/includes/aggregation.php
Outdated
'pipeline' => $pipeline, | ||
'cursor' => new stdClass() | ||
] | ||
]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider the following to utilize MongoDB\Collection::explain()
:
$aggregate = new MongoDB\Operation\Aggregate(
$collection->getDatabaseName(),
$collection->getCollectionName(),
$pipeline
);
$result = $collection->explain($aggregate);
Note that Collection::explain()
returns a single document instead of a cursor, so there's no need to call toArray()
.
source/aggregation.txt
Outdated
API Documentation | ||
~~~~~~~~~~~~~~~~~ | ||
|
||
For more information about executing aggregation operations by using the {+php-library+}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would "with the" read better than "by using the"? There's already an active verb with "executing".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have style guidelines about with / by using: https://www.mongodb.com/docs/meta/style-guide/terminology/alphabetical-terms/#std-term-using
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional small suggestions I missed previously.
source/includes/aggregation.php
Outdated
); | ||
|
||
$result = $collection->explain($aggregate); | ||
echo json_encode($result) . PHP_EOL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: I dunno offhand if explain
output might include more complex BSON types, but since this is essentially debugging output I don't think we need to be pedantic about using the driver's BSON functions to print actual extended JSON.
source/includes/aggregation.php
Outdated
$cursor = $collection->aggregate($pipeline); | ||
|
||
foreach ($cursor as $doc) { | ||
echo json_encode($doc) . PHP_EOL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
echo
supports any number of comma-delimited arguments, so you could also consider:
echo json_encode($doc) . PHP_EOL; | |
echo json_encode($doc), PHP_EOL; |
This avoids string concatenation and would be slightly more efficient.
Also applies to echo
for the explain output. I might have missed this in previous PRs, so up to you if you'd like to address. OK to leave as-is if a bunch of docs pages are already doing . PHP_EOL
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll change to a comma here and add that to the cleanup tasks
* Add PR template * Scaffolding * DOCSP-41953: Create a deployment (#93) * DOCSP-41953: Create a deployment * toc * add troubleshoot file * DOCSP-41954: Create a connection string (#99) * DOCSP-41954: Create a connection string * edits * spacing * DOCSP-42388: Next steps (#101) * DOCSP-41975: Retrieve data (#102) * DOCSP-41975: Retrieve data * fixes * quotes * code fix * JS feedback * JT feedback * code format * fix * JT feedback 2 * edit * DOCSP-41952: Download and Install (#92) * DOCSP-41952: Download and Install * edits * toc, fixes * AS feedback * AS feedback 2 * typo * JT feedback * DOCSP-41976: Projection guide (#104) * DOCSP-41976: Projection guide * edits * fixes * code edits, output * JS feedback * JT feedback * DOCSP-41955: Connect to MongoDB (#100) * DOCSP-41955: Connect to MongoDB * edits * edit * RR feedback * typo * code edit * DOCSP-41974: Specify a query (#103) * DOCSP-41974: Specify a query * edits * output * fix * snooty.toml * RR feedback * code edits * DOCSP-41977: Specify documents to return (#105) * DOCSP-41977: Specify documents to return * edits * code output * toc * MM feedback, code edits * edit * JT feedback * edit * test * MM feedback 2 * DOCSP-41957 - Mongo Client (#106) Co-authored-by: Rea Rustagi <85902999+rustagir@users.noreply.github.com> * DOCSP-41960 - TLS (#114) * DOCSP-41978: Count documents (#108) * DOCSP-41978: Count documents * edits * code ex format * link * RR feedback * DOCSP-41979: Distinct values (#109) Adds a Distinct Values guide. * DOCSP-41981: Change streams (#113) Adds a Change Streams guide. * DOCSP-41993 Compatibility Table * test webhook * toc and sharedincludes * lang table * edits * headings * retry on shared include * shared includes * callout * help links * DOCSP-41958 - Connection Targets (#107) Co-authored-by: Nora Reidy <nora.reidy@mongodb.com> Co-authored-by: Andreas Braun <git@alcaeus.org> * tech review * DOCSP-41968: Update documents (#118) * DOCSP-41968: Update documents * snooty fix * edits * DOCSP-41967: Insert documents (#116) * DOCSP-41967: Insert documents * build * snooty * edit * JS feedback * JT feedback * JT feedback 2 * DOCSP-41980: Cursor guide (#110) * DOCSP-41980: Cursor guide * edits * code edits * output * vale fix * MM feedback * edit * fix code ex * edit * JT feedback * DOCSP-41988: Aggregation (#115) * DOCSP-41988: Aggregation * toc * edits * code edit * JS feedback * dev center * JM feedback * explain api link * JM feedback 2 * DOCSP-41983: indexes landing pg * wip * wip * MM PR fixes 1 * JM tech review 1 * JM tech review 1 * wip * wip * DOCSP-41969: Replace documents (#125) * DOCSP-41969: Replace documents * edits * wording * SA * JT feedback * JM tech review 2 * Add files via upload * DOCSP-41970: Delete documents (#128) Adds a guide that shows how to delete documents. * DOCSP-41984: single field index * wip * JS PR fixes 1 * wip * DOCSP-41986: multikey indexes * links * bullet pts * JS suggestion * fix whitespace per JM comment * uncomment * DOCSP-41985: compound idx * small fix * DOCSP-41966: Write operations landing (#135) * DOCSP-41966: Write operations landing * edits * RR feedback * JT feedback * JT feedback 2 * gridfs examples * DOCSP-42026: In use encryption (#142) * DOCSP-42026: In use encryption * edit * DOCSP-41972: GridFS guide (#133) * DOCSP-41972: GridFS guide * fixes * code edits * fix * RR feedback * phpmethod * fix * JM most feedback * alternate upload/download methods * file revisions * code fix * tojson * edits * JM feedback 2 * edits * JM last feedback * DOCSP-41971: Bulk write (#130) * DOCSP-41971: Bulk write * edits * JS feedback * api links * DOCSP-41987: atlas search idx * resolve todos * toc * DOCSP-41963: Databases and collections (#136) * DOCSP-41963: Databases and collections * edits * phpmethod * code fix * MW feedback * fix * MW feedback 2 * JM feedback * JM feedback 2 * JS fix * internal review * DOCSP-41982: cluster monitoring * small fixes * MW PR fixes 1 * test netlify * spacing * try using out of repo ref and add back legend * ou of repo ref * legend glitch * JM tech review 1 * single quotes * links * vale * remove older driver version past 1.15 * DOCSP-41991 What's New * add rest of versions * fix links * fix links * JM tech review 2 * DOCSP-41964: Time series collections (#138) * DOCSP-41964: Time series collections * toc * edits * keywords * SA feedback * JT feedback * JM final fixes * review comments * typo * DOCSP-41973 Read Landing Page * review comments * vale errors * small thing * full page with ex * add page * meta * DOCSP-41965: Read and write settings (#146) * DOCSP-41965: Read and write settings * more info * edits * edits * headers * fix link * fix * RR feedback * api docs * fix * fix * DOCSP-41950: Landing page (#150) * DOCSP-41950: Landing page * fix build errors * fixes, data formats * toc * fix * data formats folder * RR feedback * fix * remove file * edit sample app intro * change links in count sections * update sample app intro * DOCSP-41990: Authentication mechanisms (#139) * DOCSP-41990: Authentication mechanisms * client tabs * edits * edits * add info * reduce repetition * add section * fix link * MW feedback * fix * JM most feedback * move to code file * more JM edits * JM feedback 2 * DOCSP-41989: Security landing page (#149) * DOCSP-41989: Security landing page * more info * edits * snooty.toml * edits * RR feedback * JM feedback * DOCSP-41962 - Stable API (#117) Co-authored-by: Jeremy Mikola <jmikola@gmail.com> * DOCSP-41992 Upgrade versions * toc * edits * how to upgrade sections * style * edit * edit * review comments * ref * Revise descriptions for server opening/closed events * DOCSP-43204: Connection landing page (#147) * DOCSP-43204: Connection landing page * toc edit * edits * remove compression * fix * sample app * snooty * JM feedback * replica set * JM feedback 2 * JM last feedback * DOCSP-43819: php 1.20 release * fix * DOCSP-41956: run a command * wip * formatting fix * JS PR fixes 1 * link fix * style fixes * JT tech review 1 * wip * JM tech review * tree * api links * links * JM tech review 2 * small fixes * add ext upgrade command * DOCSP-41995: transaction * wip * update code * NR PR fixes 1 * wip * add emphasis * add with_txn() method to api links * cxn string env * edit * JM tech review 1 * remove dupe sc * tech review * wrap fix * edit copy * DOCSP-41959 - Connection Options (#112) Co-authored-by: Nora Reidy <nora.reidy@mongodb.com> Co-authored-by: Jeremy Mikola <jmikola@gmail.com> * DOCSP-43396: Cleanup (#151) * DOCSP-43396: Cleanup * quickstart fix * code fixes * edit * snooty * edit * code output * build log errors * another build fix * add info * upgrade guide to landing * fix * driver mentions * RR feedback * build fix * build fix * php directives --------- Co-authored-by: Mike Woofter <108414937+mongoKart@users.noreply.github.com> Co-authored-by: Rea Rustagi <85902999+rustagir@users.noreply.github.com> Co-authored-by: Lindsey Moore <lindsey.moore@mongodb.com> Co-authored-by: Andreas Braun <git@alcaeus.org> Co-authored-by: rustagir <rea.rustagi@mongodb.com> Co-authored-by: Brandon Ly <brandonly@lostcoding.com> Co-authored-by: lindseymoore <71525840+lindseymoore@users.noreply.github.com> Co-authored-by: Jeremy Mikola <jmikola@gmail.com>
Pull Request Info
PR Reviewing Guidelines
JIRA - https://jira.mongodb.org/browse/DOCSP-41988
Staging - https://preview-mongodbnorareidy.gatsbyjs.io/php-library/DOCSP-41988-aggregation/aggregation/
Self-Review Checklist