Skip to content

PHPLIB-402: Add aggregate helper to Database class #642

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

Merged
merged 2 commits into from
Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,70 +1,32 @@
arg_name: option
name: allowDiskUse
type: boolean
description: |
Enables writing to temporary files. When set to ``true``, aggregation stages
can write data to the ``_tmp`` sub-directory in the ``dbPath`` directory. The
default is ``false``.
interface: phpmethod
operation: ~
optional: true
source:
file: apiargs-aggregate-option.yaml
ref: allowDiskUse
---
arg_name: option
name: batchSize
type: integer
description: |
Specifies the initial batch size for the cursor. A batchSize of ``0`` means an
empty first batch and is useful for quickly returning a cursor or failure
message without doing significant server-side work.
interface: phpmethod
operation: ~
optional: true
source:
file: apiargs-aggregate-option.yaml
ref: batchSize
---
source:
file: apiargs-MongoDBCollection-common-option.yaml
file: apiargs-aggregate-option.yaml
ref: bypassDocumentValidation
post: |
This only applies when using the :ref:`$out <agg-out>` stage.

Document validation requires MongoDB 3.2 or later: if you are using an earlier
version of MongoDB, this option will be ignored.
---
arg_name: option
name: comment
type: string
description: |
Users can specify an arbitrary string to help trace the operation through the
database profiler, currentOp, and logs.

source:
file: apiargs-aggregate-option.yaml
ref: comment
post: |
.. versionadded:: 1.3
interface: phpmethod
operation: ~
optional: true
---
arg_name: option
name: explain
type: boolean
description: |
Specifies whether or not to return the information on the processing of the
pipeline.

source:
file: apiargs-aggregate-option.yaml
ref: explain
post: |
.. versionadded:: 1.4
interface: phpmethod
operation: ~
optional: true
---
arg_name: option
name: hint
type: string|array|object
description: |
The index to use. Specify either the index name as a string or the index key
pattern as a document. If specified, then the query system will only consider
plans using the hinted index.

source:
file: apiargs-aggregate-option.yaml
ref: hint
post: |
.. versionadded:: 1.3
interface: phpmethod
operation: ~
optional: true
---
source:
file: apiargs-common-option.yaml
Expand Down Expand Up @@ -96,6 +58,12 @@ type: boolean
description: |
Indicates whether the command will request that the server provide results
using a cursor. The default is ``true``.

.. note::

MongoDB 3.6+ no longer supports returning results without a cursor (excluding
when the explain option is used) and specifying false for this option will
result in a server error.
interface: phpmethod
operation: ~
optional: true
Expand Down
55 changes: 55 additions & 0 deletions docs/includes/apiargs-MongoDBDatabase-method-aggregate-option.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
source:
file: apiargs-aggregate-option.yaml
ref: allowDiskUse
---
source:
file: apiargs-aggregate-option.yaml
ref: batchSize
---
source:
file: apiargs-aggregate-option.yaml
ref: bypassDocumentValidation
---
source:
file: apiargs-aggregate-option.yaml
ref: comment
---
source:
file: apiargs-aggregate-option.yaml
ref: explain
---
source:
file: apiargs-aggregate-option.yaml
ref: hint
---
source:
file: apiargs-common-option.yaml
ref: maxTimeMS
---
source:
file: apiargs-MongoDBDatabase-common-option.yaml
ref: readConcern
---
source:
file: apiargs-MongoDBDatabase-common-option.yaml
ref: readPreference
post: |
This option will be ignored when using the :ref:`$out <agg-out>` stage.
---
source:
file: apiargs-common-option.yaml
ref: session
---
source:
file: apiargs-MongoDBDatabase-common-option.yaml
ref: typeMap
---
source:
file: apiargs-MongoDBDatabase-common-option.yaml
ref: writeConcern
post: |
This only applies when the :ref:`$out <agg-out>` stage is specified.

This is not supported for server versions prior to 3.4 and will result in an
exception at execution time if used.
...
14 changes: 14 additions & 0 deletions docs/includes/apiargs-MongoDBDatabase-method-aggregate-param.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
arg_name: param
name: $pipeline
type: array
description: |
Specifies an :manual:`aggregation pipeline </core/aggregation-pipeline>`
operation.
interface: phpmethod
operation: ~
optional: false
---
source:
file: apiargs-common-param.yaml
ref: $options
...
62 changes: 62 additions & 0 deletions docs/includes/apiargs-aggregate-option.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
arg_name: option
name: allowDiskUse
type: boolean
description: |
Enables writing to temporary files. When set to ``true``, aggregation stages
can write data to the ``_tmp`` sub-directory in the ``dbPath`` directory. The
default is ``false``.
interface: phpmethod
operation: ~
optional: true
---
arg_name: option
name: batchSize
type: integer
description: |
Specifies the initial batch size for the cursor. A batchSize of ``0`` means an
empty first batch and is useful for quickly returning a cursor or failure
message without doing significant server-side work.
interface: phpmethod
operation: ~
optional: true
---
source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: bypassDocumentValidation
post: |
This only applies when using the :ref:`$out <agg-out>` stage.

Document validation requires MongoDB 3.2 or later: if you are using an earlier
version of MongoDB, this option will be ignored.
---
arg_name: option
name: comment
type: string
description: |
Users can specify an arbitrary string to help trace the operation through the
database profiler, currentOp, and logs.
interface: phpmethod
operation: ~
optional: true
---
arg_name: option
name: explain
type: boolean
description: |
Specifies whether or not to return the information on the processing of the
pipeline.
interface: phpmethod
operation: ~
optional: true
---
arg_name: option
name: hint
type: string|array|object
description: |
The index to use. Specify either the index name as a string or the index key
pattern as a document. If specified, then the query system will only consider
plans using the hinted index.
interface: phpmethod
operation: ~
optional: true
...
1 change: 1 addition & 0 deletions docs/reference/class/MongoDBDatabase.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Methods

/reference/method/MongoDBDatabase__construct
/reference/method/MongoDBDatabase__get
/reference/method/MongoDBDatabase-aggregate
/reference/method/MongoDBDatabase-command
/reference/method/MongoDBDatabase-createCollection
/reference/method/MongoDBDatabase-drop
Expand Down
3 changes: 2 additions & 1 deletion docs/reference/method/MongoDBCollection-aggregate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Errors/Exceptions
.. include:: /includes/extracts/error-invalidargumentexception.rst
.. include:: /includes/extracts/error-driver-runtimeexception.rst

.. _php-agg-method-behavior:
.. _php-coll-agg-method-behavior:

Behavior
--------
Expand All @@ -63,6 +63,7 @@ value will be :php:`Traversable <traversable>`.
See Also
--------

- :phpmethod:`MongoDB\\Database::aggregate()`
- :manual:`aggregate </reference/command/aggregate>` command reference in the
MongoDB manual
- :manual:`Aggregation Pipeline </core/aggregation-pipeline>` documentation in
Expand Down
63 changes: 63 additions & 0 deletions docs/reference/method/MongoDBDatabase-aggregate.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
==============================
MongoDB\\Database::aggregate()
==============================

.. versionadded:: 1.5

.. default-domain:: mongodb

.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol

Definition
----------

.. phpmethod:: MongoDB\\Database::aggregate()

Runs a specified :manual:`admin/diagnostic pipeline
</reference/operator/aggregation-pipeline/#db-aggregate-stages>` which does
not require an underlying collection. For aggregations on collection data,
see :phpmethod:`MongoDB\\Collection::aggregate()`.

.. code-block:: php

function aggregate(array $pipeline, array $options = []): Traversable

This method has the following parameters:

.. include:: /includes/apiargs/MongoDBDatabase-method-aggregate-param.rst

The ``$options`` parameter supports the following options:

.. include:: /includes/apiargs/MongoDBDatabase-method-aggregate-option.rst

Return Values
-------------

A :php:`MongoDB\\Driver\\Cursor <class.mongodb-driver-cursor>` or
:php:`ArrayIterator <arrayiterator>` object. In both cases, the return value
will be :php:`Traversable <traversable>`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to suggest double-checking to see if explain is compatible with any of the DB-level stages, but even if none of those are supported it's possible something is introduced in the future that does work with explain and might result in an ArrayIterator returned. I think we can leave this as-is. It's still accurate, even without the verbose "Behavior" section left in.


Errors/Exceptions
-----------------

.. include:: /includes/extracts/error-unexpectedvalueexception.rst
.. include:: /includes/extracts/error-unsupportedexception.rst
.. include:: /includes/extracts/error-invalidargumentexception.rst
.. include:: /includes/extracts/error-driver-runtimeexception.rst

.. _php-db-agg-method-behavior:

.. todo: add examples

See Also
--------

- :phpmethod:`MongoDB\\Collection::aggregate()`
- :manual:`aggregate </reference/command/aggregate>` command reference in the
MongoDB manual
- :manual:`Aggregation Pipeline </core/aggregation-pipeline>` documentation in
the MongoDB Manual
2 changes: 1 addition & 1 deletion docs/tutorial/crud.txt
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ The |php-library|\'s :phpmethod:`MongoDB\\Collection::aggregate()` method
returns a :php:`Traversable <traversable>` object, which you can iterate upon to
access the results of the aggregation operation. Refer to the
:phpmethod:`MongoDB\\Collection::aggregate()` method's :ref:`behavior
reference <php-agg-method-behavior>` for more about the method's output.
reference <php-coll-agg-method-behavior>` for more about the method's output.

The following example lists the 5 US states with the most zip codes associated
with them:
Expand Down
Loading