Cloud Datastore Client - Class AggregationQuery (1.23.0) Stay organized with collections Save and categorize content based on your preferences.
Reference documentation and code samples for the Cloud Datastore Client class AggregationQuery.
Represents anAggregation Query.
Example:
use Google\Cloud\Datastore\DatastoreClient;use Google\Cloud\Datastore\Query\Aggregation;$datastore = new DatastoreClient();$query = $datastore->query();$query->kind('Companies');$query->filter('companyName', '=', 'Google');$aggregationQuery = $query->aggregation(Aggregation::count()->alias('total'));$res = $datastore->runAggregationQuery($aggregationQuery);echo $res->get('total');Example (aggregated using over method):
use Google\Cloud\Datastore\DatastoreClient;use Google\Cloud\Datastore\Query\Aggregation;$datastore = new DatastoreClient();$query = $datastore->query();$query->kind('Companies');$query->filter('companyName', '=', 'Google');$query->limit(100);$aggregationQuery = $datastore->aggregationQuery();$aggregationQuery->over($query)->addAggregation(Aggregation::count()->alias('total_upto_100'));$res = $datastore->runAggregationQuery($aggregationQuery);echo $res->get('total_upto_100');Namespace
Google \ Cloud \ Datastore \ QueryMethods
__construct
Create an aggregation query.
| Parameters | |
|---|---|
| Name | Description |
query | Google\Cloud\Datastore\Query\QueryInterface|null |
aggregates | mixed |
addAggregation
Adds a Query Aggregation.
Accepts an array of properties for aggregation.
Example:
$query = $datastore->AggregationQuery();$query->kind('Companies');$query->filter('companyName', '=', 'Google');$query->addAggregation(Aggregation::count()->alias('total'));echo json_encode($query->queryObject());| Parameter | |
|---|---|
| Name | Description |
aggregation | Google\Cloud\Datastore\Query\AggregationThe Aggregation to be included. |
| Returns | |
|---|---|
| Type | Description |
Google\Cloud\Datastore\Query\AggregationQuery | |
over
Set the Query Projection.
Accepts an array of properties. If set, only these properties will bereturned.
Example:
$query = $datastore->query();$query->kind('Companies');$query->filter('companyName', '=', 'Google');$pipeline = $datastore->AggregationQuery() ->over($query) ->addAggregation(Aggregation::count()->alias('total'));| Parameter | |
|---|---|
| Name | Description |
query | Google\Cloud\Datastore\Query\QueryInterfaceThe query whose properties to include. |
| Returns | |
|---|---|
| Type | Description |
Google\Cloud\Datastore\Query\AggregationQuery | |
queryObject
Format the query for use in the API.
| Returns | |
|---|---|
| Type | Description |
array | |
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-01-24 UTC.