Cloud Datastore Client - Class Query (1.28.2) Stay organized with collections Save and categorize content based on your preferences.
Reference documentation and code samples for the Cloud Datastore Client class Query.
Represents a CloudDatastore Query
Queries can be created either by using the builder pattern, or by providingaQuerywhen creating this object.
Example:
use Google\Cloud\Datastore\DatastoreClient;$datastore = new DatastoreClient();$query = $datastore->query();$query->kind('Companies');$query->filter('companyName', '=', 'Google');$res = $datastore->runQuery($query);foreach ($res as $company) { echo $company['companyName']; // Google}// Queries can also be constructed using a// [Query Object](https://cloud.google.com/datastore/reference/rest/v1/projects/runQuery#query):$query = $datastore->query([ 'query' => [ 'kind' => [ [ 'name' => 'Companies' ] ], 'filter' => [ 'propertyFilter' => [ 'op' => 'EQUAL', 'property' => [ 'name' => 'companyName' ], 'value' => [ 'stringValue' => 'Google' ] ] ] ]]);Namespace
Google \ Cloud \ Datastore \ QueryMethods
__construct
| Parameters | |
|---|---|
| Name | Description |
entityMapper | Google\Cloud\Datastore\EntityMapperAn instance of EntityMapper |
query | arrayoptional(https://cloud.google.com/datastore/reference/rest/v1/projects/runQuery#query) |
projection
Set the Query Projection.
Accepts an array of properties. If set, only these properties will bereturned.
Example:
$query->projection(['firstName', 'lastName']);| Parameter | |
|---|---|
| Name | Description |
properties | array|stringThe property or properties to include inthe result. |
| Returns | |
|---|---|
| Type | Description |
Google\Cloud\Datastore\Query\Query | |
keysOnly
Set the query to return only keys (no properties).
Example:
$query->keysOnly();| Returns | |
|---|---|
| Type | Description |
Google\Cloud\Datastore\Query\Query | |
kind
Set the Kind to query.
If empty, returns entities of all kinds. Must be set in order to filterresults. While you may supply as many kinds as you wish, datastore currentlyonly accepts one at a time.
Example:
$query->kind('Person');| Parameter | |
|---|---|
| Name | Description |
kinds | array|stringThe kind or kinds to return. Only a single kindis currently supported. |
| Returns | |
|---|---|
| Type | Description |
Google\Cloud\Datastore\Query\Query | |
filter
See also:
| Parameters | |
|---|---|
| Name | Description |
filterOrProperty | string|arrayEither a string property name oran array representation of Property/Composite filter returnedby Filter::and(), Filter::or() and Filter::where(). |
operator | string|null[optional] The operator to use in the filterif property name is used in the first argument. A list ofallowed operators may be foundhere.Short comparison operators are provided for convenience and aremapped to their datastore-compatible equivalents. Available shortoperators are |
value | mixed[optional] The value to check if property name isused in the first argument. |
| Returns | |
|---|---|
| Type | Description |
Google\Cloud\Datastore\Query\Query | |
hasAncestor
Query for entities by their ancestors.
Keys can be provided either via aGoogle\Cloud\Datastore\Keyobject, or by providing a kind, identifier and (optionally) an identifiertype.
Example:
$key = $datastore->key('Person', 'Bob');$query->hasAncestor($key);// Specifying an identifier type$key = $datastore->key('Robots', '1337', [ 'identifierType' => Key::TYPE_NAME]);$query->hasAncestor($key);| Parameter | |
|---|---|
| Name | Description |
key | Google\Cloud\Datastore\KeyThe ancestor Key instance. |
| Returns | |
|---|---|
| Type | Description |
Google\Cloud\Datastore\Query\Query | |
order
See also:
| Parameters | |
|---|---|
| Name | Description |
property | stringThe property to order by. |
direction | string[optional] The direction to order in. GoogleCloud PHP provides class constants which map to allowed Datastorevalues. Those constants are |
| Returns | |
|---|---|
| Type | Description |
Google\Cloud\Datastore\Query\Query | |
distinctOn
The properties to make distinct.
The query results will contain the first result for each distinctcombination of values for the given properties (if empty, all resultsare returned).
Example:
$query->distinctOn('lastName');| Parameter | |
|---|---|
| Name | Description |
property | array|stringThe property or properties to make distinct. |
| Returns | |
|---|---|
| Type | Description |
Google\Cloud\Datastore\Query\Query | |
start
See also:
| Parameter | |
|---|---|
| Name | Description |
cursor | stringThe cursor on which to start the result. |
| Returns | |
|---|---|
| Type | Description |
Google\Cloud\Datastore\Query\Query | |
end
See also:
| Parameter | |
|---|---|
| Name | Description |
cursor | stringThe cursor on which to end the result. |
| Returns | |
|---|---|
| Type | Description |
Google\Cloud\Datastore\Query\Query | |
offset
See also:
| Parameter | |
|---|---|
| Name | Description |
num | intThe number of results to skip. |
| Returns | |
|---|---|
| Type | Description |
Google\Cloud\Datastore\Query\Query | |
limit
See also:
| Parameter | |
|---|---|
| Name | Description |
num | intThe number of results to return. |
| Returns | |
|---|---|
| Type | Description |
Google\Cloud\Datastore\Query\Query | |
canPaginate
Indicate that this type does support automatic pagination.
| Returns | |
|---|---|
| Type | Description |
bool | |
queryObject
Return a service-compliant array.
| Returns | |
|---|---|
| Type | Description |
array | |
queryKey
Return the query_type union field name.
| Returns | |
|---|---|
| Type | Description |
string | |
aggregation
| Parameter | |
|---|---|
| Name | Description |
aggregation | Google\Cloud\Datastore\Query\Aggregation |
jsonSerialize
Constants
OP_DEFAULT
Value: self::OP_EQUALSOP_LESS_THAN
Value: 'LESS_THAN'OP_LESS_THAN_OR_EQUAL
Value: 'LESS_THAN_OR_EQUAL'OP_GREATER_THAN
Value: 'GREATER_THAN'OP_GREATER_THAN_OR_EQUAL
Value: 'GREATER_THAN_OR_EQUAL'OP_EQUALS
Value: 'EQUAL'OP_NOT_EQUALS
Value: 'NOT_EQUAL'OP_IN
Value: 'IN'OP_NOT_IN
Value: 'NOT_IN'OP_HAS_ANCESTOR
Value: 'HAS_ANCESTOR'ORDER_DEFAULT
Value: self::ORDER_ASCENDINGORDER_DESCENDING
Value: 'DESCENDING'ORDER_ASCENDING
Value: 'ASCENDING'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.