Go 1.11 telah mencapai akhir dukungan
dan akan
dihentikan penggunaannya
pada 31 Januari 2026. Setelah penghentian penggunaan, Anda tidak akan dapat men-deploy aplikasi Go 1.11, meskipun organisasi Anda sebelumnya menggunakan kebijakan organisasi untuk mengaktifkan kembali deployment runtime lama. Aplikasi Go 1.11 yang ada akan terus berjalan dan menerima traffic setelah
tanggal penghentiannya. Sebaiknya Anda
bermigrasi ke Go versi terbaru yang didukung.
Mengambil hasil kueri
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Setelah mengonstruksi kueri, Anda dapat menentukan sejumlah opsi pengambilan untuk
mengontrol lebih lanjut hasil yang ditampilkan.
Lihat kueri datastore untuk informasi selengkapnya tentang membuat struktur kueri untuk aplikasi Anda.
Melakukan iterasi melalui hasil kueri
Saat melakukan iterasi hasil kueri menggunakan metode Run
dari nilai Query
, Cloud Datastore mengambil hasilnya dalam batch. Secara default, setiap batch berisi 20 hasil. Anda dapat terus melakukan iterasi melalui hasil kueri hingga semua hasil kueri ditampilkan atau waktu permintaan habis.
Untuk melakukan iterasi pada setiap entity yang cocok dengan kueri Anda, gunakan metode
Run
untuk mendapatkan
Iterator
, yang dapat digunakan untuk menelusuri setiap entity menggunakan metode
Next
Iterator
.
Untuk mengambil sekaligus semua entity yang cocok dengan kueri Anda, gunakan metode GetAll
.
Mengambil properti yang dipilih dari entity
Untuk hanya mengambil properti entity yang dipilih, bukan seluruh entity, gunakan kueri proyeksi. Jenis kueri ini berjalan lebih cepat dan biayanya lebih murah dibandingkan kueri yang menampilkan entity lengkap.
Demikian pula, kueri khusus kunci menghemat waktu dan resource dengan hanya menampilkan kunci ke entity yang cocok, bukan entity lengkap. Untuk membuat jenis kueri ini, panggil metode KeysOnly
saat mengonstruksi Query
.:
Menetapkan batas untuk kueri Anda
Anda dapat menentukan batas kueri untuk mengontrol jumlah maksimum hasil yang ditampilkan dalam satu batch. Contoh berikut mengambil lima orang tertinggi dari Cloud Datastore:
Apa langkah selanjutnya?
Kecuali dinyatakan lain, konten di halaman ini dilisensikan berdasarkan Lisensi Creative Commons Attribution 4.0, sedangkan contoh kode dilisensikan berdasarkan Lisensi Apache 2.0. Untuk mengetahui informasi selengkapnya, lihat Kebijakan Situs Google Developers. Java adalah merek dagang terdaftar dari Oracle dan/atau afiliasinya.
Terakhir diperbarui pada 2025-09-04 UTC.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-09-04 UTC."],[[["\u003cp\u003eThis API is designed for first-generation runtimes, with a migration guide available for upgrading to App Engine Go 1.12+ second-generation runtimes.\u003c/p\u003e\n"],["\u003cp\u003eQuery results can be iterated through using the \u003ccode\u003eRun\u003c/code\u003e method, which retrieves results in batches, or retrieved all at once with the \u003ccode\u003eGetAll\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003eProjection queries allow for the retrieval of selected properties from an entity, which is faster and more cost-effective than retrieving full entities.\u003c/p\u003e\n"],["\u003cp\u003eKeys-only queries, created by calling the \u003ccode\u003eKeysOnly\u003c/code\u003e method, save resources by only returning the keys of matching entities.\u003c/p\u003e\n"],["\u003cp\u003eYou can limit the number of results returned by a query in a single batch by specifying a limit using the \u003ccode\u003eLimit\u003c/code\u003e method.\u003c/p\u003e\n"]]],[],null,["# Retrieving query results\n\n| This API is supported for first-generation runtimes and can be used when [upgrading to corresponding second-generation runtimes](/appengine/docs/standard/\n| go\n| /services/access). If you are updating to the App Engine Go 1.12+ runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/go-differences) to learn about your migration options for legacy bundled services.\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/go111/datastore/queries) for more information on structuring queries for your app.\n\nIterating through query results\n-------------------------------\n\nWhen iterating through the results of a query using the [`Run`](/appengine/docs/legacy/standard/go111/datastore/reference#Query.Run) method of a [`Query`](/appengine/docs/legacy/standard/go111/datastore/reference#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.\nTo iterate over each entity that matches your query, use the [`Run`](/appengine/docs/legacy/standard/go111/datastore/reference#Query.Run) method to obtain an [`Iterator`](/appengine/docs/legacy/standard/go111/datastore/reference#Iterator), with which you can step through each entity using the `Iterator`'s [`Next`](/appengine/docs/legacy/standard/go111/datastore/reference#Iterator.Next) method.\n\n\u003cbr /\u003e\n\n q := datastore.NewQuery(\"Person\")\n t := q.Run(ctx)\n for {\n \tvar p Person\n \tk, err := t.Next(&p)\n \tif err == datastore.Done {\n \t\tbreak // No further entities match the query.\n \t}\n \tif err != nil {\n \t\tlog.Errorf(ctx, \"fetching next Person: %v\", err)\n \t\tbreak\n \t}\n \t// Do something with Person p and Key k\n \tdoSomething(k, p)\n }\n\nTo retrieve all entities matching your query at once, use the [`GetAll`](/appengine/docs/legacy/standard/go111/datastore/reference#Query.GetAll) method. \n\n q := datastore.NewQuery(\"Person\")\n var people []Person\n keys, err := q.GetAll(ctx, &people)\n if err != nil {\n \tlog.Errorf(ctx, \"fetching people: %v\", err)\n \treturn\n }\n for i, p := range people {\n \tk := keys[i]\n \t// Do something with Person p and Key k\n \tdoSomething(k, p)\n }\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/go111/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/go111/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, call the `KeysOnly` method when constructing the `Query`.: \n\n q := datastore.NewQuery(\"Person\").KeysOnly()\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 := datastore.NewQuery(\"Person\").Order(\"-Height\").Limit(5)\n var people []Person\n _, err := q.GetAll(ctx, &people)\n // check err\n\n for _, p := range people {\n \tlog.Infof(ctx, \"%s %s, %d inches tall\", p.FirstName, p.LastName, p.Height)\n }\n\nWhat's next?\n------------\n\n- Learn the [common restrictions](/appengine/docs/legacy/standard/go111/datastore/query-restrictions) for queries on Cloud Datastore.\n- Learn about [query cursors](/appengine/docs/legacy/standard/go111/datastore/query-cursors), which allow an application to retrieve a query's results in convenient batches.\n- [Understand data consistency](/appengine/docs/legacy/standard/go111/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/go111/datastore/queries) for Cloud Datastore."]]