Firestore ドキュメントに対してバッチ アップデートを実行する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Firestore ドキュメントに対してバッチ アップデートを実行する
もっと見る
このコードサンプルを含む詳細なドキュメントについては、以下をご覧ください。
コードサンプル
C#
Firestore に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。
Go
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 perform a batch update on a Firestore document, allowing multiple operations to be executed as a single atomic unit.\u003c/p\u003e\n"],["\u003cp\u003eThe code examples show how to set data for a document, update a field in an existing document, and delete a document within a single batch.\u003c/p\u003e\n"],["\u003cp\u003eThe batch operations are performed using \u003ccode\u003eSet\u003c/code\u003e, \u003ccode\u003eUpdate\u003c/code\u003e, and \u003ccode\u003eDelete\u003c/code\u003e commands on a document reference, all within the same batch.\u003c/p\u003e\n"],["\u003cp\u003eTo apply changes to Firestore, the batch needs to be committed using the \u003ccode\u003eCommit\u003c/code\u003e command.\u003c/p\u003e\n"],["\u003cp\u003eExamples in the page cover C#, Go, Java, Node.js, PHP, Python, and Ruby.\u003c/p\u003e\n"]]],[],null,["Performs a batch update on a Firestore document\n\nExplore further\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Transactions and batched writes](/firestore/native/docs/manage-data/transactions)\n- [Transactions and batched writes](https://firebase.google.com/docs/firestore/manage-data/transactions)\n\nCode sample \n\nC#\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 WriteBatch batch = db.StartBatch();\n\n // Set the data for NYC\n DocumentReference nycRef = db.Collection(\"cities\").Document(\"NYC\");\n Dictionary\u003cstring, object\u003e nycData = new Dictionary\u003cstring, object\u003e\n {\n { \"name\", \"New York City\" }\n };\n batch.Set(nycRef, nycData);\n\n // Update the population for SF\n DocumentReference sfRef = db.Collection(\"cities\").Document(\"SF\");\n Dictionary\u003cstring, object\u003e updates = new Dictionary\u003cstring, object\u003e\n {\n { \"Population\", 1000000}\n };\n batch.Update(sfRef, updates);\n\n // Delete LA\n DocumentReference laRef = db.Collection(\"cities\").Document(\"LA\");\n batch.Delete(laRef);\n\n // Commit the batch\n await batch.CommitAsync();\n\nGo\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 import (\n \t\"context\"\n \t\"log\"\n\n \t\"cloud.google.com/go/firestore\"\n )\n\n func batchWrite(ctx context.Context, client *firestore.Client) error {\n \t// Get a new write batch.\n \tbatch := client.https://cloud.google.com/go/docs/reference/cloud.google.com/go/firestore/latest/index.html#cloud_google_com_go_firestore_Client_Batch()\n\n \t// Set the value of \"NYC\".\n \tnycRef := client.Collection(\"cities\").Doc(\"NYC\")\n \tbatch.Set(nycRef, map[string]interface{}{\n \t\t\"name\": \"New York City\",\n \t})\n\n \t// Update the population of \"SF\".\n \tsfRef := client.Collection(\"cities\").Doc(\"SF\")\n \tbatch.Set(sfRef, map[string]interface{}{\n \t\t\"population\": 1000000,\n \t}, firestore.https://cloud.google.com/go/docs/reference/cloud.google.com/go/firestore/latest/index.html#cloud_google_com_go_firestore_MergeAll)\n\n \t// Delete the city \"LA\".\n \tlaRef := client.Collection(\"cities\").Doc(\"LA\")\n \tbatch.Delete(laRef)\n\n \t// Commit the batch.\n \t_, err := batch.Commit(ctx)\n \tif err != nil {\n \t\t// Handle any errors in an appropriate way, such as returning them.\n \t\tlog.Printf(\"An error has occurred: %s\", err)\n \t}\n\n \treturn err\n }\n\nJava\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 // Get a new write batch\n WriteBatch batch = db.batch();\n\n // Set the value of 'NYC'\n DocumentReference nycRef = db.collection(\"cities\").document(\"NYC\");\n batch.set(nycRef, new City());\n\n // Update the population of 'SF'\n DocumentReference sfRef = db.collection(\"cities\").document(\"SF\");\n batch.update(sfRef, \"population\", 1000000L);\n\n // Delete the city 'LA'\n DocumentReference laRef = db.collection(\"cities\").document(\"LA\");\n batch.delete(laRef);\n\n // asynchronously commit the batch\n ApiFuture\u003cList\u003cWriteResult\u003e\u003e future = batch.commit();\n // ...\n // future.get() blocks on batch commit operation\n for (WriteResult result : future.get()) {\n System.out.println(\"Update time : \" + result.getUpdateTime());\n }\n\nNode.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 // Get a new write batch\n const batch = db.batch();\n\n // Set the value of 'NYC'\n const nycRef = db.collection('cities').doc('NYC');\n batch.set(nycRef, {name: 'New York City'});\n\n // Update the population of 'SF'\n const sfRef = db.collection('cities').doc('SF');\n batch.update(sfRef, {population: 1000000});\n\n // Delete the city 'LA'\n const laRef = db.collection('cities').doc('LA');\n batch.delete(laRef);\n\n // Commit the batch\n await batch.commit();\n\nPHP\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 $batch = $db-\u003ebatch();\n\n # Set the data for NYC\n $nycRef = $db-\u003ecollection('samples/php/cities')-\u003edocument('NYC');\n $batch-\u003eset($nycRef, [\n 'name' =\u003e 'New York City'\n ]);\n\n # Update the population for SF\n $sfRef = $db-\u003ecollection('samples/php/cities')-\u003edocument('SF');\n $batch-\u003eupdate($sfRef, [\n ['path' =\u003e 'population', 'value' =\u003e 1000000]\n ]);\n\n # Delete LA\n $laRef = $db-\u003ecollection('samples/php/cities')-\u003edocument('LA');\n $batch-\u003edelete($laRef);\n\n # Commit the batch\n $batch-\u003ecommit();\n\nPython\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 batch = db.batch()\n\n # Set the data for NYC\n nyc_ref = db.collection(\"cities\").document(\"NYC\")\n batch.set(nyc_ref, {\"name\": \"New York City\"})\n\n # Update the population for SF\n sf_ref = db.collection(\"cities\").document(\"SF\")\n batch.update(sf_ref, {\"population\": 1000000})\n\n # Delete DEN\n den_ref = db.collection(\"cities\").document(\"DEN\")\n batch.delete(den_ref)\n\n # Commit the batch\n batch.commit()\n\nRuby\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 firestore.batch do |b|\n # Set the data for NYC\n b.set \"#{collection_path}/NYC\", { name: \"New York City\" }\n\n # Update the population for SF\n b.update \"#{collection_path}/SF\", { population: 1_000_000 }\n\n # Delete LA\n b.delete \"#{collection_path}/LA\"\n end\n\nWhat's next\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=firestore)."]]