Requête composée avec des filtres de plage et d'inégalité sur plusieurs champs
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Veuillez fournir des exemples en C# et Ruby qui correspondent aux exemples Firestore et Datastore initialement demandés dans le plan de documentation Firestore pour les inégalités multiples à la disponibilité générale et le bug 351980346.
La demande concerne turbo/469184.
En savoir plus
Pour obtenir une documentation détaillée incluant cet exemple de code, consultez les articles suivants :
Exemple de code
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis page provides C#, Go, Java, PHP, Python, and Ruby code samples demonstrating how to perform Firestore queries with multiple inequality filters, specifically for population greater than 1,000,000 and density less than 10,000 (or 5,000 in the Ruby example).\u003c/p\u003e\n"],["\u003cp\u003eThe provided code samples showcase the implementation of these queries using \u003ccode\u003eWhereGreaterThan\u003c/code\u003e and \u003ccode\u003eWhereLessThan\u003c/code\u003e in languages such as C# or \u003ccode\u003ewhere\u003c/code\u003e when using PHP, Python, Ruby, and Go, on multiple fields, as requested in turbo/469184.\u003c/p\u003e\n"],["\u003cp\u003eAll code samples instruct users to set up Application Default Credentials for authentication to Firestore, directing to a link with information on how to proceed.\u003c/p\u003e\n"],["\u003cp\u003eThe documentation related to these code examples, specifically, querying with range and inequality filters on multiple fields, can be found via the provided links to further reading material.\u003c/p\u003e\n"],["\u003cp\u003eFurther code samples for other Google Cloud products, including Firestore, are available in the Google Cloud sample browser, as mentioned at the end of the documentation.\u003c/p\u003e\n"]]],[],null,["# Compound query with range and inequality filters on multiple fields\n\nPlease provide C# and Ruby samples that match Firestore and Datastore examples originally requested in the Firestore Doc Plan for Multiple Inequalities to GA and bug 351980346.\nThe request is for turbo/469184.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Query with range and inequality filters on multiple fields overview](/firestore/native/docs/query-data/multiple-range-fields)\n- [Query with range and inequality filters on multiple fields overview](https://firebase.google.com/docs/firestore/query-data/multiple-range-fields)\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 Query query = citiesRef\n .WhereGreaterThan(\"Population\", 1000000)\n .WhereLessThan(\"Density\", 10000);\n QuerySnapshot querySnapshot = await query.GetSnapshotAsync();\n foreach (DocumentSnapshot documentSnapshot in querySnapshot)\n {\n var name = documentSnapshot.GetValue\u003cstring\u003e(\"Name\");\n var population = documentSnapshot.GetValue\u003cint\u003e(\"Population\");\n var density = documentSnapshot.GetValue\u003cint\u003e(\"Density\");\n Console.WriteLine($\"City '{name}' returned by query. Population={population}; Density={density}\");\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 import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n\n \t\"cloud.google.com/go/firestore\"\n )\n\n func multipleInequalitiesQuery(w io.Writer, projectID string) error {\n \tctx := context.Background()\n\n \t// Create client\n \tclient, err := firestore.NewClient(ctx, projectID)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"firestore.NewClient: %w\", err)\n \t}\n \tdefer client.Close()\n\n \t// Create query\n \tquery := client.Collection(\"cities\").\n \t\thttps://cloud.google.com/go/docs/reference/cloud.google.com/go/firestore/latest/index.html#cloud_google_com_go_firestore_Query_Where(\"population\", \"\u003e\", 1000000).\n \t\thttps://cloud.google.com/go/docs/reference/cloud.google.com/go/firestore/latest/index.html#cloud_google_com_go_firestore_Query_Where(\"density\", \"\u003c\", 10000)\n\n \t// Get documents\n \tdocSnapshots, err := query.Documents(ctx).GetAll()\n \tfor _, doc := range docSnapshots {\n \t\tfmt.Fprintln(w, doc.https://cloud.google.com/go/docs/reference/cloud.google.com/go/firestore/latest/index.html#cloud_google_com_go_firestore_DocumentSnapshot_Data())\n \t}\n \tif err != nil {\n \t\treturn fmt.Errorf(\"GetAll: %w\", err)\n \t}\n\n \treturn nil\n }\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 =\n db.collection(\"cities\")\n .whereGreaterThan(\"population\", 1000000)\n .whereLessThan(\"density\", 10000);\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 $collection = $db-\u003ecollection('samples/php/cities');\n $chainedQuery = $collection\n -\u003ewhere('population', '\u003e', 1000000)\n -\u003ewhere('density', '\u003c', 10000);\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 query = (\n db.collection(\"cities\")\n .where(filter=FieldFilter(\"population\", \"\u003e\", 1_000_000))\n .where(filter=FieldFilter(\"density\", \"\u003c\", 10_000))\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 cities_ref = firestore.col collection_path\n compound_multi_ineq_query = cities_ref.where(\"population\", \"\u003e\", 1_000_000).where(\"density\", \"\u003c\", 5_000)\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)."]]