# Query

- [JSON representation](https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#SCHEMA_REPRESENTATION)
- [Projection](https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#Projection)
  - [JSON representation](https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#Projection.SCHEMA_REPRESENTATION)
- [KindExpression](https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#KindExpression)
  - [JSON representation](https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#KindExpression.SCHEMA_REPRESENTATION)
- [Filter](https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#Filter)
  - [JSON representation](https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#Filter.SCHEMA_REPRESENTATION)
- [CompositeFilter](https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#CompositeFilter)
  - [JSON representation](https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#CompositeFilter.SCHEMA_REPRESENTATION)
- [Operator](https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#Operator)
- [PropertyFilter](https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#PropertyFilter)
  - [JSON representation](https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#PropertyFilter.SCHEMA_REPRESENTATION)
- [Operator](https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#Operator_1)
- [PropertyOrder](https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#PropertyOrder)
  - [JSON representation](https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#PropertyOrder.SCHEMA_REPRESENTATION)
- [Direction](https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#Direction)

A query for entities.

The query stages are executed in the following order: 1. kind 2. filter 3. projection 4. order + startCursor + endCursor 5. offset 6. limit 7. findNearest

| JSON representation |
|---|
| ``` { "projection": [ { object (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#Projection`) } ], "kind": [ { object (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#KindExpression`) } ], "filter": { object (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#Filter`) }, "order": [ { object (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#PropertyOrder`) } ], "distinctOn": [ { object (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/PropertyReference`) } ], "startCursor": string, "endCursor": string, "offset": integer, "limit": integer } ``` |

| Fields ||
|---|---|
| `projection[]` | ``object (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#Projection`)`` The projection to return. Defaults to returning all properties. |
| `kind[]` | ``object (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#KindExpression`)`` The kinds to query (if empty, returns entities of all kinds). Currently at most 1 kind may be specified. |
| `filter` | ``object (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#Filter`)`` The filter to apply. |
| `order[]` | ``object (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#PropertyOrder`)`` The order to apply to the query results (if empty, order is unspecified). |
| `distinctOn[]` | ``object (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/PropertyReference`)`` The properties to make distinct. The query results will contain the first result for each distinct combination of values for the given properties (if empty, all results are returned). Requires: - If `order` is specified, the set of distinct on properties must appear before the non-distinct on properties in `order`. |
| `startCursor` | `string (https://developers.google.com/discovery/v1/type-format format)` A starting point for the query results. Query cursors are returned in query result batches and [can only be used to continue the same query](https://cloud.google.com/datastore/docs/concepts/queries#cursors_limits_and_offsets). A base64-encoded string. |
| `endCursor` | `string (https://developers.google.com/discovery/v1/type-format format)` An ending point for the query results. Query cursors are returned in query result batches and [can only be used to limit the same query](https://cloud.google.com/datastore/docs/concepts/queries#cursors_limits_and_offsets). A base64-encoded string. |
| `offset` | `integer` The number of results to skip. Applies before limit, but after all other constraints. Optional. Must be \>= 0 if specified. |
| `limit` | `integer` The maximum number of results to return. Applies after all other constraints. Optional. Unspecified is interpreted as no limit. Must be \>= 0 if specified. |

## Projection

A representation of a property in a projection.

| JSON representation |
|---|
| ``` { "property": { object (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/PropertyReference`) } } ``` |

| Fields ||
|---|---|
| `property` | ``object (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/PropertyReference`)`` The property to project. |

## KindExpression

A representation of a kind.

| JSON representation |
|---|
| ``` { "name": string } ``` |

| Fields ||
|---|---|
| `name` | `string` The name of the kind. |

## Filter

A holder for any type of filter.

| JSON representation |
|---|
| ``` { // Union field `filter_type` can be only one of the following: "compositeFilter": { object (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#CompositeFilter`) }, "propertyFilter": { object (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#PropertyFilter`) } // End of list of possible types for union field `filter_type`. } ``` |

| Fields ||
|---|---|
| Union field `filter_type`. The type of filter. `filter_type` can be only one of the following: ||
| `compositeFilter` | ``object (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#CompositeFilter`)`` A composite filter. |
| `propertyFilter` | ``object (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#PropertyFilter`)`` A filter on a property. |

## CompositeFilter

A filter that merges multiple other filters using the given operator.

| JSON representation |
|---|
| ``` { "op": enum (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#Operator`), "filters": [ { object (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#Filter`) } ] } ``` |

| Fields ||
|---|---|
| `op` | ``enum (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#Operator`)`` The operator for combining multiple filters. |
| `filters[]` | ``object (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#Filter`)`` The list of filters to combine. Requires: - At least one filter is present. |

## Operator

A composite filter operator.

| Enums ||
|---|---|
| `OPERATOR_UNSPECIFIED` | Unspecified. This value must not be used. |
| `AND` | The results are required to satisfy each of the combined filters. |
| `OR` | Documents are required to satisfy at least one of the combined filters. |

## PropertyFilter

A filter on a specific property.

| JSON representation |
|---|
| ``` { "property": { object (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/PropertyReference`) }, "op": enum (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#Operator_1`), "value": { object (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/Shared.Types/Value`) } } ``` |

| Fields ||
|---|---|
| `property` | ``object (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/PropertyReference`)`` The property to filter by. |
| `op` | ``enum (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#Operator_1`)`` The operator to filter by. |
| `value` | ``object (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/Shared.Types/Value`)`` The value to compare the property to. |

## Operator

A property filter operator.

| Enums ||
|---|---|
| `OPERATOR_UNSPECIFIED` | Unspecified. This value must not be used. |
| `LESS_THAN` | The given `property` is less than the given `value`. Requires: - That `property` comes first in `order_by`. |
| `LESS_THAN_OR_EQUAL` | The given `property` is less than or equal to the given `value`. Requires: - That `property` comes first in `order_by`. |
| `GREATER_THAN` | The given `property` is greater than the given `value`. Requires: - That `property` comes first in `order_by`. |
| `GREATER_THAN_OR_EQUAL` | The given `property` is greater than or equal to the given `value`. Requires: - That `property` comes first in `order_by`. |
| `EQUAL` | The given `property` is equal to the given `value`. |
| `IN` | The given `property` is equal to at least one value in the given array. Requires: - That `value` is a non-empty `ArrayValue`, subject to disjunction limits. - No `NOT_IN` is in the same query. |
| `NOT_EQUAL` | The given `property` is not equal to the given `value`. Requires: - No other `NOT_EQUAL` or `NOT_IN` is in the same query. - That `property` comes first in the `order_by`. |
| `HAS_ANCESTOR` | Limit the result set to the given entity and its descendants. Requires: - That `value` is an entity key. - All evaluated disjunctions must have the same `HAS_ANCESTOR` filter. |
| `NOT_IN` | The value of the `property` is not in the given array. Requires: - That `value` is a non-empty `ArrayValue` with at most 10 values. - No other `OR`, `IN`, `NOT_IN`, `NOT_EQUAL` is in the same query. - That `field` comes first in the `order_by`. |

## PropertyOrder

The desired order for a specific property.

| JSON representation |
|---|
| ``` { "property": { object (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/PropertyReference`) }, "direction": enum (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#Direction`) } ``` |

| Fields ||
|---|---|
| `property` | ``object (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/PropertyReference`)`` The property to order by. |
| `direction` | ``enum (`https://docs.cloud.google.com/datastore/docs/reference/data/rest/v1/Query#Direction`)`` The direction to order by. Defaults to `ASCENDING`. |

## Direction

The sort direction.

| Enums ||
|---|---|
| `DIRECTION_UNSPECIFIED` | Unspecified. This value must not be used. |
| `ASCENDING` | Ascending. |
| `DESCENDING` | Descending. |