Abfrageergebnisse abrufen
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Nachdem Sie eine Abfrage erstellt haben, können Sie verschiedene Abrufoptionen festlegen, um die gelieferten Ergebnisse noch gezielter einzugrenzen.
Weitere Informationen zum Strukturieren von Abfragen für Ihre Anwendung finden Sie unter Datastore-Abfragen.
Einzelne Entität abrufen
Zum Abrufen einer einzelnen Entität, die mit Ihrer Abfrage übereinstimmt, verwenden Sie die Methode Query.get()
(oder GqlQuery.get()
):
q = Person.all()
q.filter("last_name =", target_last_name)
result = q.get()
Dies gibt das erste Ergebnis im Index zurück, das mit der Abfrage übereinstimmt.
Abfrageergebnisse durchlaufen
Beim Durchsuchen der Ergebnisse einer Abfrage mithilfe der run()
-Methode eines Query
- oder GqlQuery
-Objekts ruft Cloud Datastore die Ergebnisse in Batches ab. Standardmäßig enthält jeder Batch 20 Ergebnisse. Sie können diesen Wert jedoch mit dem Parameter batch_size
der Methode ändern. Sie können die Abfrageergebnisse durchlaufen, bis alle Ergebnisse zurückgegeben sind oder eine Zeitüberschreitung eintritt.
Ausgewählte Attribute einer Entität abrufen
Wenn Sie anstatt einer vollständigen Entität nur eine Auswahl der zugehörigen Attribute abrufen möchten, verwenden Sie eine Projektionsabfrage. Dieser Abfragetyp wird schneller ausgeführt und kostet weniger als Abfragen, die vollständige Entitäten zurückgeben.
Analog dazu können Sie mit ausschließlich schlüsselbasierten Abfragen Zeit und Ressourcen sparen, da damit nur die Schlüssel für die übereinstimmenden Entitäten zurückgegeben werden und nicht die vollständigen Entitäten. Sie verwenden die Methode keys_only=True
, um diese Art von Abfrage zu erstellen:
q = Person.all(keys_only=True)
Limits für Abfragen festlegen
Sie können ein Limit für die Abfrage festlegen, um die Anzahl der in einem Batch zurückgegebenen Ergebnisse zu begrenzen. Im folgenden Beispiel werden die fünf größten Personen aus Cloud Datastore abgerufen:
q = Person.all()
q.order("-height")
for p in q.run(limit=5):
print "%s %s, %d inches tall" % (p.first_name, p.last_name, p.height)
Weitere Informationen
Sofern nicht anders angegeben, sind die Inhalte dieser Seite unter der Creative Commons Attribution 4.0 License und Codebeispiele unter der Apache 2.0 License lizenziert. Weitere Informationen finden Sie in den Websiterichtlinien von Google Developers. Java ist eine eingetragene Marke von Oracle und/oder seinen Partnern.
Zuletzt aktualisiert: 2025-09-04 (UTC).
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-09-04 (UTC)."],[[["\u003cp\u003eYou can refine query results by using various retrieval options to control the data returned.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eQuery.get()\u003c/code\u003e method allows for the retrieval of a single entity that matches the query criteria.\u003c/p\u003e\n"],["\u003cp\u003eQuery results can be iterated through in batches, with the default batch size being 20, which can be adjusted with the \u003ccode\u003ebatch_size\u003c/code\u003e parameter.\u003c/p\u003e\n"],["\u003cp\u003eProjection queries allow you to retrieve only selected properties of an entity, enhancing query speed and efficiency, while keys-only queries only return the keys to the entities, saving time and resources.\u003c/p\u003e\n"],["\u003cp\u003eA limit can be set on queries to restrict the number of results returned in a single batch, useful for controlling the amount of data processed.\u003c/p\u003e\n"]]],[],null,["# Retrieving query results\n\nAfter constructing a query, you can specify a number of retrieval options to\nfurther control the results it returns.\nSee [datastore queries](/appengine/docs/legacy/standard/python/datastore/queries) for more information on structuring queries for your app.\n\nRetrieving a single entity\n--------------------------\n\n\u003cbr /\u003e\n\nTo retrieve just a single entity matching your query, use the method [`Query.get()`](/appengine/docs/legacy/standard/python/datastore/queryclass#Query_get) (or [`GqlQuery.get()`](/appengine/docs/legacy/standard/python/datastore/gqlqueryclass#GqlQuery_get)): \n\n q = Person.all()\n q.filter(\"last_name =\", target_last_name)\n\n result = q.get()\n\nThis returns the first result found in the index that matches the query.\n\n\nIterating through query results\n-------------------------------\n\nWhen iterating through the results of a query using the `run()` method of a [`Query`](/appengine/docs/legacy/standard/python/datastore/queryclass#Query_run) or [`GqlQuery`](/appengine/docs/legacy/standard/python/datastore/gqlqueryclass#GqlQuery_run) object, Cloud Datastore retrieves the results in batches. By default each batch contains 20 results, but you can change this value using the method's `batch_size` parameter. You can continue iterating through query results until all are returned or the request times out.\n\nRetrieving selected properties from an entity\n---------------------------------------------\n\nTo retrieve only selected properties of an entity rather than the entire entity, use a [*projection query*](/appengine/docs/legacy/standard/python/datastore/projectionqueries). This type of query runs faster and costs less than one that returns complete entities.\n\nSimilarly, a [*keys-only query*](/appengine/docs/legacy/standard/python/datastore/queries#keys-only_queries) 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, set `keys_only=True` when constructing the query object: \n\n q = Person.all(keys_only=True)\n\nSetting a limit for your query\n------------------------------\n\nYou 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: \n\n q = Person.all()\n q.order(\"-height\")\n\n for p in q.run(limit=5):\n print \"%s %s, %d inches tall\" % (p.first_name, p.last_name, p.height)\n\nWhat's next?\n------------\n\n- Learn the [common restrictions](/appengine/docs/legacy/standard/python/datastore/query-restrictions) for queries on Cloud Datastore.\n- Learn about [query cursors](/appengine/docs/legacy/standard/python/datastore/query-cursors), which allow an application to retrieve a query's results in convenient batches.\n- [Understand data consistency](/appengine/docs/legacy/standard/python/datastore/data-consistency) and how data consistency works with different types of queries on Cloud Datastore.\n- Learn the [basic syntax and structure of queries](/appengine/docs/legacy/standard/python/datastore/queries) for Cloud Datastore."]]