Kueri gabungan dengan filter rentang dan ketidaksetaraan di beberapa kolom
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Berikan contoh C# dan Ruby yang cocok dengan contoh Firestore dan Datastore yang awalnya diminta dalam Rencana Dokumen Firestore untuk Beberapa Ketidaksetaraan ke GA dan bug 351980346.
Permintaan ini adalah untuk turbo/469184.
Mempelajari lebih lanjut
Untuk dokumentasi mendetail yang menyertakan contoh kode ini, lihat artikel berikut:
Contoh kode
Kecuali dinyatakan lain, konten di halaman ini dilisensikan berdasarkan Lisensi Creative Commons Attribution 4.0, sedangkan contoh kode dilisensikan berdasarkan Lisensi Apache 2.0. Untuk mengetahui informasi selengkapnya, lihat Kebijakan Situs Google Developers. Java adalah merek dagang terdaftar dari Oracle dan/atau afiliasinya.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","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)."]]