Java 8 has reached end of support
and will be
deprecated
on January 31, 2026. After deprecation, you won't be able to deploy Java 8
applications, even if your organization previously used an organization policy to
re-enable deployments of legacy runtimes. Your existing Java
8 applications will continue to run and receive traffic after their
deprecation date. We recommend that
you
migrate to the latest supported version of Java.
Retrieving query results
Stay organized with collections
Save and categorize content based on your preferences.
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.
Retrieving a single entity
To retrieve just a single entity matching your query, use the method PreparedQuery.asSingleEntity()
:
This returns the first result found in the index that matches the query.
(If there is more than one matching result, it throws a TooManyResultsException
.)
Iterating through query results
When iterating through the results of a query using the PreparedQuery.asIterable()
and PreparedQuery.asIterator()
methods, Cloud Datastore retrieves the results in batches. By default each batch contains 20 results, but you can change this value using FetchOptions.chunkSize()
. You can continue iterating through query results until all are returned or the request times out.
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, use the Query.setKeysOnly()
method:
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?
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-21 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-21 UTC."],[[["\u003cp\u003eThis API supports first-generation runtimes and is applicable when upgrading to corresponding second-generation runtimes, while migration to Java 11/17 runtimes requires referring to a specific migration guide.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003ePreparedQuery.asSingleEntity()\u003c/code\u003e retrieves a single entity matching a query, throwing a \u003ccode\u003eTooManyResultsException\u003c/code\u003e if multiple matches exist.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003ePreparedQuery.asIterable()\u003c/code\u003e and \u003ccode\u003ePreparedQuery.asIterator()\u003c/code\u003e allow iterating through query results in batches, with the batch size adjustable via \u003ccode\u003eFetchOptions.chunkSize()\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eProjection queries retrieve only selected properties, and keys-only queries retrieve only keys, both being faster and less resource-intensive than full-entity queries.\u003c/p\u003e\n"],["\u003cp\u003eA limit can be set on queries to control the maximum number of results, as demonstrated in the example that retrieves the five tallest people.\u003c/p\u003e\n"]]],[],null,[]]