複数のフィールドに対する範囲フィルタと不等式フィルタを使用した複合クエリ
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Firestore Doc Plan for Multiple Inequalities to GA とバグ 351980346 で最初にリクエストした Firestore と Datastore の例に一致する C# と Ruby のサンプルを提供してください。
turbo/469184 に対するリクエストです。
もっと見る
このコードサンプルを含む詳細なドキュメントについては、以下をご覧ください。
コードサンプル
C#
Firestore に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。
Go
Firestore に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。
Java
Firestore に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。
PHP
Firestore に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。
Python
Firestore に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。
Ruby
Firestore に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","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)."]]