Zusammengesetzte Abfrage mit Bereichs- und Ungleichheitsfiltern für mehrere Felder
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Bitte stellen Sie C#- und Ruby-Beispiele bereit, die den ursprünglich im Firestore-Dokumentplan für mehrere Ungleichheiten bis zur allgemeinen Verfügbarkeit und im Fehler 351980346 angeforderten Firestore- und Datastore-Beispielen entsprechen.
Die Anfrage bezieht sich auf turbo/469184.
Weitere Informationen
Eine ausführliche Dokumentation, die dieses Codebeispiel enthält, finden Sie hier:
Codebeispiel
Nächste Schritte
Wenn Sie nach Codebeispielen für andere Google Cloud -Produkte suchen und filtern möchten, können Sie den Google Cloud -Beispielbrowser verwenden.
Sofern nicht anders angegeben, sind die Inhalte dieser Seite unter der Creative Commons Attribution 4.0 License und Codebeispiele unter der Apache 2.0 License lizenziert. Weitere Informationen finden Sie in den Websiterichtlinien von Google Developers. Java ist eine eingetragene Marke von Oracle und/oder seinen Partnern.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","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)."]]