After constructing a query, you can specify a number of retrieval options to further control the results it returns. See datastore queries for more information on structuring queries for your app.
Iterating through query results
When iterating through the results of a query using the Run
method of a Query
value, Cloud Datastore retrieves the results in batches. By default each batch contains 20 results. You can continue iterating through query results until all are returned or the request times out.
Run
method to obtain an Iterator
, with which you can step through each entity using the Iterator
's Next
method.
To retrieve all entities matching your query at once, use the GetAll
method.
Retrieving selected properties from an entity
To retrieve only selected properties of an entity rather than the entire entity, use a projection query. This type of query runs faster and costs less than one that returns complete entities.
Similarly, a keys-only query saves time and resources by returning just the keys to the entities it matches, rather than the full entities themselves. To create this type of query, call the KeysOnly
method when constructing the Query
.:
Setting a limit for your query
You can specify a limit for your query to control the maximum number of results returned in one batch. The following example retrieves the five tallest people from Cloud Datastore:
What's next?
- Learn the common restrictions for queries on Cloud Datastore.
- Learn about query cursors, which allow an application to retrieve a query's results in convenient batches.
- Understand data consistency and how data consistency works with different types of queries on Cloud Datastore.
- Learn the basic syntax and structure of queries for Cloud Datastore.