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');
Namespace
Google \ Cloud \ Datastore \ QueryMethods
__construct
Create an aggregation query.
Parameters | |
---|---|
Name | Description |
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 |
Aggregation
The Aggregation to be included. |
Returns | |
---|---|
Type | Description |
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 | |
---|---|
Name | Description |
query |
QueryInterface
The query whose properties to include. |
Returns | |
---|---|
Type | Description |
AggregationQuery |
queryObject
Format the query for use in the API.
Returns | |
---|---|
Type | Description |
array |