Query composta con filtri di intervallo e disuguaglianza su più campi
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Fornisci esempi di C# e Ruby che corrispondano agli esempi di Firestore e Datastore originariamente richiesti nel piano di documentazione di Firestore per più disuguaglianze in GA e nel bug 351980346.
La richiesta riguarda turbo/469184.
Per saperne di più
Per la documentazione dettagliata che include questo esempio di codice, vedi quanto segue:
Esempio di codice
Salvo quando diversamente specificato, i contenuti di questa pagina sono concessi in base alla licenza Creative Commons Attribution 4.0, mentre gli esempi di codice sono concessi in base alla licenza Apache 2.0. Per ulteriori dettagli, consulta le norme del sito di Google Developers. Java è un marchio registrato di Oracle e/o delle sue consociate.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","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)."]]