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