Actualiza un documento de Firestore que contiene un campo de array
Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
Actualiza un documento de Firestore que contiene un campo de array.
Explora más
Para obtener documentación en la que se incluye esta muestra de código, consulta lo siguiente:
Muestra de código
Salvo que se indique lo contrario, el contenido de esta página está sujeto a la licencia Atribución 4.0 de Creative Commons, y los ejemplos de código están sujetos a la licencia Apache 2.0. Para obtener más información, consulta las políticas del sitio de Google Developers. Java es una marca registrada de Oracle o sus afiliados.
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Información o código de muestra incorrectos","incorrectInformationOrSampleCode","thumb-down"],["Faltan la información o los ejemplos que necesito","missingTheInformationSamplesINeed","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Otro","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)."]]