使用 Query 值的 Run 方法迭代查詢結果時,Cloud Datastore 會分批擷取結果。根據預設,每個批次會包含 20 個結果。您可以繼續反覆查詢查詢結果,直到傳回所有結果或是要求逾時為止。
如要逐一檢查符合查詢的每個實體,請使用 Run 方法取得 Iterator,然後使用 Iterator 的 Next 方法逐一檢查每個實體。
q:=datastore.NewQuery("Person")t:=q.Run(ctx)for{varpPersonk,err:=t.Next(&p)iferr==datastore.Done{break// No further entities match the query.}iferr!=nil{log.Errorf(ctx,"fetching next Person: %v",err)break}// Do something with Person p and Key kdoSomething(k,p)}
q:=datastore.NewQuery("Person")varpeople[]Personkeys,err:=q.GetAll(ctx,&people)iferr!=nil{log.Errorf(ctx,"fetching people: %v",err)return}fori,p:=rangepeople{k:=keys[i]// Do something with Person p and Key kdoSomething(k,p)}
[[["容易理解","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-07-08 (世界標準時間)。"],[[["This API is designed for first-generation runtimes, with a migration guide available for upgrading to App Engine Go 1.12+ second-generation runtimes."],["Query results can be iterated through using the `Run` method, which retrieves results in batches, or retrieved all at once with the `GetAll` method."],["Projection queries allow for the retrieval of selected properties from an entity, which is faster and more cost-effective than retrieving full entities."],["Keys-only queries, created by calling the `KeysOnly` method, save resources by only returning the keys of matching entities."],["You can limit the number of results returned by a query in a single batch by specifying a limit using the `Limit` method."]]],[]]