Python 2.7 已終止支援,並將於 2026 年 1 月 31 日
淘汰。淘汰後,您將無法部署 Python 2.7 應用程式,即使貴機構先前曾使用機構政策重新啟用舊版執行階段的部署作業,也無法部署。現有的 Python 2.7 應用程式在
淘汰日期過後,仍會繼續執行並接收流量。建議您
改用系統支援的最新 Python 版本。
擷取查詢結果
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
建構查詢後,您可以指定多個擷取選項,進一步控制傳回的結果。如要進一步瞭解如何建構應用程式的查詢,請參閱資料儲存庫查詢。
擷取單一實體
如果只想擷取與查詢相符的單一實體,請使用 Query.get()
(或 GqlQuery.get()
) 方法:
q = Person.all()
q.filter("last_name =", target_last_name)
result = q.get()
這會傳回第一個在索引中找到,且與查詢相符的結果。
疊代查詢結果
使用 Query
或 GqlQuery
物件的 run()
方法迭代查詢結果時,Cloud Datastore 會分批擷取結果。根據預設,每個批次會包含 20 個結果,不過您可使用方法的 batch_size
參數變更每個批次中的結果數量。您可以繼續反覆查詢查詢結果,直到傳回所有結果或是要求逾時為止。
從實體上擷取所選的屬性
如果只要擷取實體的部分屬性 (而非整個實體),請使用投影查詢。相較於傳回完整的實體,這類查詢執行速度較快,費用也較低。
同樣的,純索引鍵查詢可以只傳回相符實體的索引鍵,而非整個實體,藉此節省時間和資源。如要建立這類查詢,請在建構查詢物件時設定 keys_only=True
:
q = Person.all(keys_only=True)
設定查詢限制
您可以指定查詢的「限制」,以此控管一批次傳回的結果上限數量。下列範例會從 Cloud Datastore 中擷取五位身高最高的人:
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)
後續步驟
- 瞭解在 Cloud Datastore 中查詢的常見限制。
- 瞭解查詢游標,應用程式可運用這項功能分批擷取查詢結果,十分方便。
- 瞭解資料的一致性,以及如何將資料一致性運用於不同類型的 Cloud Datastore 查詢。
- 瞭解 Cloud Datastore 查詢的基本語法與結構。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-09-04 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[[["\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."]]