Aggiorna un documento Firestore contenente un campo di array
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Aggiorna un documento Firestore contenente un campo di array.
Per saperne di più
Per la documentazione dettagliata che include questo esempio di codice, vedi quanto segue:
Esempio di codice
Salvo quando diversamente specificato, i contenuti di questa pagina sono concessi in base alla licenza Creative Commons Attribution 4.0, mentre gli esempi di codice sono concessi in base alla licenza Apache 2.0. Per ulteriori dettagli, consulta le norme del sito di Google Developers. Java è un marchio registrato di Oracle e/o delle sue consociate.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","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)."]]