Consulta una colección de Firestore con un filtro cursor start at document
Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
Consulta una colección de Firestore con un filtro cursor start at document
Explora más
Para obtener documentación en la que se incluye esta muestra de código, consulta lo siguiente:
Muestra de código
Salvo que se indique lo contrario, el contenido de esta página está sujeto a la licencia Atribución 4.0 de Creative Commons, y los ejemplos de código están sujetos a la licencia Apache 2.0. Para obtener más información, consulta las políticas del sitio de Google Developers. Java es una marca registrada de Oracle o sus afiliados.
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Información o código de muestra incorrectos","incorrectInformationOrSampleCode","thumb-down"],["Faltan la información o los ejemplos que necesito","missingTheInformationSamplesINeed","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Otro","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)."]]