Cloud Datastore Client - Class AggregationQuery (1.24.4)

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 \ Query

Methods

__construct

Create an aggregation query.

Parameters
NameDescription
queryGoogle\Cloud\Datastore\Query\QueryInterface|null
aggregatesmixed

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
NameDescription
aggregationGoogle\Cloud\Datastore\Query\Aggregation

The Aggregation to be included.

Returns
TypeDescription
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
NameDescription
queryGoogle\Cloud\Datastore\Query\QueryInterface

The query whose properties to include.

Returns
TypeDescription
Google\Cloud\Datastore\Query\AggregationQuery

queryObject

Format the query for use in the API.

Returns
TypeDescription
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.