Ordering a Firestore query with a filter
Stay organized with collections
Save and categorize content based on your preferences.
Ordering a Firestore query with a filter
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis page demonstrates how to order a Firestore query while applying a filter based on a field's value.\u003c/p\u003e\n"],["\u003cp\u003eThe examples show filtering for cities with a population greater than 2,500,000 and ordering the results by population.\u003c/p\u003e\n"],["\u003cp\u003eCode samples are provided for C#, Go, Java, Node.js, PHP, Python, and Ruby to illustrate the query structure.\u003c/p\u003e\n"],["\u003cp\u003eAuthenticating to Firestore is necessary, and setting up Application Default Credentials is the first step in the process.\u003c/p\u003e\n"],["\u003cp\u003eYou can find additional code samples and filters for Google Cloud products in the Google Cloud sample browser.\u003c/p\u003e\n"]]],[],null,["# Ordering a Firestore query with a filter\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 Query query = citiesRef\n .WhereGreaterThan(\"Population\", 2500000)\n .OrderBy(\"Population\");\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 query := cities.Where(\"population\", \"\u003e\", 2500000).OrderBy(\"population\", firestore.Asc)\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 Query query = cities.whereGreaterThan(\"population\", 2500000L).orderBy(\"population\");\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 citiesRef.where('population', '\u003e', 2500000).orderBy('population');\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 $query = $citiesRef\n -\u003ewhere('population', '\u003e', 2500000)\n -\u003eorderBy('population');\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 cities_ref = db.collection(\"cities\")\n query = cities_ref.where(filter=FieldFilter(\"population\", \"\u003e\", 2500000)).order_by(\n \"population\"\n )\n results = query.stream()\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 query = cities_ref.where(\"population\", \"\u003e\", 2_500_000).order(\"population\")\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)."]]