Mettre à jour un document Firestore contenant un champ de tableau
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Mettre à jour un document Firestore contenant un champ de tableau.
En savoir plus
Pour obtenir une documentation détaillée incluant cet exemple de code, consultez les articles suivants :
Exemple de code
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","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)."]]