Membuat kueri koleksi Firestore dengan kursor yang dimulai di filter dokumen
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Membuat kueri koleksi Firestore dengan kursor yang dimulai di filter dokumen
Mempelajari lebih lanjut
Untuk dokumentasi mendetail yang menyertakan contoh kode ini, lihat artikel berikut:
Contoh kode
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.
[[["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"]],[],[[["\u003cp\u003eThis page demonstrates how to query a Firestore collection using a cursor that starts at a specific document, effectively using a document snapshot as a starting point for the query.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples provided illustrate this technique across various programming languages, including C#, Go, Java, Node.js, PHP, Python, and Ruby, showcasing language specific syntax.\u003c/p\u003e\n"],["\u003cp\u003eEach code sample requires Firestore authentication via Application Default Credentials, as explained in the linked documentation on setting up a local development environment.\u003c/p\u003e\n"],["\u003cp\u003eThe examples all use a \u003ccode\u003ecities\u003c/code\u003e collection, with the 'SF' document, and sorting by 'population' field to demonstrate the use of \u003ccode\u003eStartAt\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe page references related documentation for further information on pagination and query cursors within Firestore.\u003c/p\u003e\n"]]],[],null,["# Query a Firestore collection with a cursor start at document filter\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Paginate data with query cursors](/firestore/native/docs/query-data/query-cursors)\n- [Paginate data with query cursors](https://firebase.google.com/docs/firestore/query-data/query-cursors)\n\nCode sample\n-----------\n\n### C#\n\n\nTo authenticate to Firestore, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n CollectionReference citiesRef = db.Collection(\"cities\");\n DocumentReference docRef = citiesRef.Document(\"SF\");\n DocumentSnapshot snapshot = await docRef.GetSnapshotAsync();\n Query query = citiesRef.OrderBy(\"Population\").StartAt(snapshot);\n\n### Go\n\n\nTo authenticate to Firestore, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n cities := client.Collection(\"cities\")\n dsnap, err := cities.Doc(\"SF\").Get(ctx)\n if err != nil {\n \tfmt.Println(err)\n }\n query := cities.OrderBy(\"population\", firestore.Asc).StartAt(dsnap.Data()[\"population\"]).Documents(ctx)\n\n### Java\n\n\nTo authenticate to Firestore, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n // Fetch the snapshot with an API call, waiting for a maximum of 30 seconds for a result.\n ApiFuture\u003cDocumentSnapshot\u003e future = db.collection(\"cities\").document(\"SF\").get();\n DocumentSnapshot snapshot = future.get(30, TimeUnit.SECONDS);\n\n // Construct the query\n Query query = db.collection(\"cities\").orderBy(\"population\").startAt(snapshot);\n\n### Node.js\n\n\nTo authenticate to Firestore, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n const docRef = db.collection('cities').doc('SF');\n const snapshot = await docRef.get();\n const startAtSnapshot = db.collection('cities')\n .orderBy('population')\n .startAt(snapshot);\n\n await startAtSnapshot.limit(10).get();\n\n### PHP\n\n\nTo authenticate to Firestore, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n $citiesRef = $db-\u003ecollection('samples/php/cities');\n $docRef = $citiesRef-\u003edocument('SF');\n $snapshot = $docRef-\u003esnapshot();\n\n $query = $citiesRef\n -\u003eorderBy('population')\n -\u003estartAt($snapshot);\n\n### Python\n\n\nTo authenticate to Firestore, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n doc_ref = db.collection(\"cities\").document(\"SF\")\n\n snapshot = doc_ref.get()\n start_at_snapshot = (\n db.collection(\"cities\").order_by(\"population\").start_at(snapshot)\n )\n\n### Ruby\n\n\nTo authenticate to Firestore, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n doc_ref = firestore.doc \"#{collection_path}/SF\"\n snapshot = doc_ref.get\n query = cities_ref.order(\"population\").start_at(snapshot)\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=firestore)."]]