配列フィールドを含む Firestore ドキュメントを更新する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
配列フィールドを含む Firestore ドキュメントを更新する
もっと見る
このコードサンプルを含む詳細なドキュメントについては、以下をご覧ください。
コードサンプル
C#
Firestore に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。
Java
Firestore に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。
Node.js
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 demonstrates how to update a Firestore document that contains an array field.\u003c/p\u003e\n"],["\u003cp\u003eYou can atomically add new elements to an array within a Firestore document using the \u003ccode\u003earrayUnion\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003eYou can atomically remove elements from an array within a Firestore document using the \u003ccode\u003earrayRemove\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003eThe code examples show how to implement these operations in C#, Java, Node.js, PHP, Python, and Ruby.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication with the Application Default Credentials is required before accessing Firestore, refer to the provided link to set it up.\u003c/p\u003e\n"]]],[],null,["# Update a Firestore document containing an array field.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Add and update data](/firestore/native/docs/manage-data/add-data)\n- [Add data to Cloud Firestore](https://firebase.google.com/docs/firestore/manage-data/add-data)\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 DocumentReference washingtonRef = db.Collection(\"cities\").Document(\"DC\");\n\n // Atomically add a new region to the \"regions\" array field.\n await washingtonRef.UpdateAsync(\"Regions\", FieldValue.ArrayUnion(\"greater_virginia\"));\n\n // Atomically remove a region from the \"regions\" array field.\n await washingtonRef.UpdateAsync(\"Regions\", FieldValue.ArrayRemove(\"east_coast\"));\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 DocumentReference washingtonRef = db.collection(\"cities\").document(\"DC\");\n\n // Atomically add a new region to the \"regions\" array field.\n ApiFuture\u003cWriteResult\u003e arrayUnion =\n washingtonRef.update(\"regions\", FieldValue.arrayUnion(\"greater_virginia\"));\n System.out.println(\"Update time : \" + arrayUnion.get());\n\n // Atomically remove a region from the \"regions\" array field.\n ApiFuture\u003cWriteResult\u003e arrayRm =\n washingtonRef.update(\"regions\", FieldValue.arrayRemove(\"east_coast\"));\n System.out.println(\"Update time : \" + arrayRm.get());\n\n### Node.js\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 // ...\n const washingtonRef = db.collection('cities').doc('DC');\n\n // Atomically add a new region to the \"regions\" array field.\n const unionRes = await washingtonRef.update({\n regions: FieldValue.arrayUnion('greater_virginia')\n });\n // Atomically remove a region from the \"regions\" array field.\n const removeRes = await washingtonRef.update({\n regions: FieldValue.arrayRemove('east_coast')\n });\n\n // To add or remove multiple items, pass multiple arguments to arrayUnion/arrayRemove\n const multipleUnionRes = await washingtonRef.update({\n regions: FieldValue.arrayUnion('south_carolina', 'texas')\n // Alternatively, you can use spread operator in ES6 syntax\n // const newRegions = ['south_carolina', 'texas']\n // regions: FieldValue.arrayUnion(...newRegions)\n });\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 $cityRef = $db-\u003ecollection('samples/php/cities')-\u003edocument('DC');\n\n // Atomically add a new region to the \"regions\" array field.\n $cityRef-\u003eupdate([\n ['path' =\u003e 'regions', 'value' =\u003e FieldValue::arrayUnion(['greater_virginia'])]\n ]);\n\n // Atomically remove a region from the \"regions\" array field.\n $cityRef-\u003eupdate([\n ['path' =\u003e 'regions', 'value' =\u003e FieldValue::arrayRemove(['east_coast'])]\n ]);\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 city_ref = db.collection(\"cities\").document(\"DC\")\n\n # Atomically add a new region to the 'regions' array field.\n city_ref.update({\"regions\": firestore.ArrayUnion([\"greater_virginia\"])})\n\n # // Atomically remove a region from the 'regions' array field.\n city_ref.update({\"regions\": firestore.ArrayRemove([\"east_coast\"])})\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 city_ref = firestore.doc \"#{collection_path}/DC\"\n\n # Atomically add a new region to the 'regions' array field.\n city_ref.update({ regions: firestore.field_array_union(\"greater_virginia\") })\n\n # Atomically remove a region from the 'regions' array field.\n city_ref.update({ regions: firestore.field_array_delete(\"east_coast\") })\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)."]]