Cloud Datastore Client - Class AggregationQuery (1.21.2)

Reference documentation and code samples for the Cloud Datastore Client class AggregationQuery.

Represents an Aggregation 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');

Methods

__construct

Create an aggregation query.

Parameters
NameDescription
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
NameDescription
aggregation Google\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 be returned.

Example:

$query = $datastore->query();
$query->kind('Companies');
$query->filter('companyName', '=', 'Google');

$pipeline = $datastore->AggregationQuery()
    ->over($query)
    ->addAggregation(Aggregation::count()->alias('total'));
Parameter
NameDescription
query Google\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