Melakukan kumpulan update pada dokumen Firestore
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Melakukan kumpulan update pada dokumen Firestore
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 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\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-----------\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 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\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\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\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 // 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\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 // 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\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 $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\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 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\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 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\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=firestore)."]]