An example of an invalid order and limit Firestore query
bookmark_borderbookmark
Stay organized with collections
Save and categorize content based on your preferences.
An example of an invalid order and limit Firestore query
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 an example of an invalid Firestore query that violates the constraint requiring range and order-by operations to be on the same field.\u003c/p\u003e\n"],["\u003cp\u003eThe provided code samples in C#, Go, Java, Node.js, Python, and Ruby showcase this invalid query where filtering by population and ordering by country are attempted.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication to Firestore in all code samples requires setting up Application Default Credentials, with a link provided for further information on this process.\u003c/p\u003e\n"],["\u003cp\u003eThe provided code samples will query a list of cities, and attempt to return cities with populations greater than 2,500,000, ordered by the country they are in.\u003c/p\u003e\n"]]],[],null,["# An example of an invalid order and limit Firestore query\n\nCode sample\n-----------\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 // Note: This is an invalid query. It violates the constraint that range\n // and order by are required to be on the same field.\n query := cities.Where(\"population\", \"\u003e\", 2500000).OrderBy(\"country\", 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(\"country\");\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('country');\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 \"country\"\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(\"country\")\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)."]]