> Portal Navigation: > > - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version. > - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages). > - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`). > - Top-level index of all portals: https://dev.wix.com/docs/llms.txt > - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt # Method name: data.items.update(dataCollectionId: string, item: WixDataItem, options: WixDataUpdateOptions) # Method Link: https://dev.wix.com/docs/sdk/business-solutions/data/items/update.md # Method Description: Updates an item in a collection. The `update()` method returns a Promise that resolves to the updated item from the specified collection. The Promise is rejected if the current user does not have update permissions for the collection. Calling the `update()` method triggers the [`beforeUpdate()`](https://dev.wix.com/docs/velo/api-reference/wix-data/hooks/before-update.md) and [`afterUpdate()`](https://dev.wix.com/docs/velo/api-reference/wix-data/hooks/after-update.md) hooks if they have been defined. The `update()` method compares the `_id` property of the specified item with the `_id` property values of the items in the specified collection. If an item in the collection has that `_id` value, `update()` replaces the item's property values with the ones in the specified item. If the existing item had properties with values and those properties were not included in the specified item, the values in those properties are lost. The item's `_updatedDate` property is also updated to the current date. Any valid JavaScript object can be used as a property value. The `update()` method maintains the structure of the specified object. For example, objects that contain nested objects, arrays, or arrays with nested objects are all added to the collection as defined. When updating an item in a collection that has a reference field, set the value of the reference field to the referenced item's `_id` value or the entire referenced item object. The maximum size of an item that you can update in a collection is 500kb. > **Notes:** > - The specified item must include an `_id` property that already exists in the collection. > - The `update()` method does not support multi-reference fields. For multi-reference fields, use `replaceReferences()` or `insertReference()`. > - [Translatable collections](https://support.wix.com/en/article/wix-multilingual-translating-cms-collection-content) do not allow insertion and modification of items when working in a non-primary language. For example, if a collection's primary language is English, and the site visitor is viewing the site in French, calling `update()` fails and issues an error. # Method Permissions: - Write Data Items # Method Permissions Scopes IDs: - SCOPE.DC-DATA.WRITE # Method Code Examples: ## Update an item with a specified ID in a collection ```javascript import { items } from "@wix/data"; async function updateItem() { const toUpdate = { "_id": "00001", "title": "Mr.", "first_name": "John", "last_name": "Doe" }; const updatedItem = await items.update("myCollection", toUpdate); console.log(updatedItem); // See below } /* item is: * * { * "_id": "00001", * "_owner": "ffdkj9c2-df8g-f9ke-lk98-4kjhfr89keedb", * "_createdDate": "2017-05-24T12:33:18.938Z", * "_updatedDate": "2017-05-24T12:33:18.938Z", * "title": "Mr.", * "first_name": "John", * "last_name": "Doe" * } */ ``` ## Get an item in a collection and update it ```javascript import { items } from "@wix/data"; /* given existing item: * * { * "_id": "00001", * "title": "Mr.", * "first_name": "John", * "last_name": "Doe" * } * */ async function updateItem() { const item = await items.get("myCollection", "00001"); item.last_name = "Smith"; // Updated last name const updatedItem = await items.update("myCollection", item); console.log(updatedItem); // See below } /* updated item: * * { * "_id": "00001", * "title": "Mr.", * "first_name": "John", * "last_name": "Smith" * } * */ ``` ## Query an item in a collection and update it ```javascript import { items } from "@wix/data"; /* given existing item: * * { * "_id": "00001", * "_owner": "ffdkj9c2-df8g-f9ke-lk98-4kjhfr89keedb", * "_createdDate": "2017-05-24T12:33:18.938Z", * "_updatedDate": "2017-05-24T12:33:18.938Z", * "title": "Mr.", * "first_name": "John", * "last_name": "Doe" * } * */ async function updateItem() { const results = await items.query("myCollection") .eq("first_name", "John") .eq("last_name", "Doe") .find(); if(results.items.length > 0) { const item = results.items[0]; item.last_name = "Smith"; // Updated last name const updatedItem = await items.update("myCollection", item); console.log(updatedItem); // See below } else { // handle case where no matching items found } } /* updated item: * * { * "_id": "00001", * "_owner": "ffdkj9c2-df8g-f9ke-lk98-4kjhfr89keedb", * "_createdDate": "2017-05-24T12:33:18.938Z", * "_updatedDate": "2017-05-24T16:13:46.545Z", * "title": "Mr.", * "first_name": "John", * "last_name": "Smith" * } * */ ``` ## Update an item including a reference to another item using the referenced item's ID ```javascript import { items } from "@wix/data"; async function updateItem() { const countryId = "id-of-usa-item"; const toUpdate = { "_id": "00001", "title": "Mr.", "first_name": "John", "last_name": "Doe", "country": countryId }; const updatedItem = await items.update("myCollection", toUpdate); console.log(updatedItem); // See below } /* item is: * * { * "_id": "rifk4nrk-dj4o-djhe-oidk-fnoqw4oiglk4i", * "_owner": "ffdkj9c2-df8g-f9ke-lk98-4kjhfr89keedb", * "_createdDate": "2017-05-24T12:33:18.938Z", * "_updatedDate": "2017-05-24T12:33:18.938Z", * "title": "Mr.", * "first_name": "John", * "last_name": "Doe", * "country": "id-of-usa-item" * } */ ``` ## Update an item including a reference to another item using the referenced item ```javascript import { items } from "@wix/data"; async function updateItem() { const countryItem = // Get country item const toUpdate = { "_id": "00001", "title": "Mr.", "first_name": "John", "last_name": "Doe", "country": countryItem }; const updatedItem = await items.update("myCollection", toUpdate); console.log(updatedItem); // See below } /* item is: * * { * "_id": "rifk4nrk-dj4o-djhe-oidk-fnoqw4oiglk4i", * "_owner": "ffdkj9c2-df8g-f9ke-lk98-4kjhfr89keedb", * "_createdDate": "2017-05-24T12:33:18.938Z", * "_updatedDate": "2017-05-24T12:33:18.938Z", * "title": "Mr.", * "first_name": "John", * "last_name": "Doe", * "country": "id-of-usa-item" * } */ ``` ## Update a collection using a custom function ```javascript import { items } from "@wix/data"; // Code for an update operation using update() // function updateGreeting(itemId, language, greeting) { const toUpdate = { _id: itemId, language, greeting }; return items.update('Greetings', toUpdate); } ``` ## Update an item in a collection with condition ```javascript import { items } from "@wix/data"; async function updateItem() { const filter = items.filter().eq("active", true); const toUpdate = { "_id": "00001", "title": "Mr.", "first_name": "John", "last_name": "Doe", "active": true }; const updatedItem = await items.update("Users", toUpdate, { condition: filter }); console.log(updatedItem); // See below } /* item is: * * { * "_id": "00001", * "_owner": "ffdkj9c2-df8g-f9ke-lk98-4kjhfr89keedb", * "_createdDate": "2017-05-24T12:33:18.938Z", * "_updatedDate": "2017-05-24T12:33:18.938Z", * "title": "Mr.", * "first_name": "John", * "last_name": "Doe", * "active": true, * } */ ``` ## default ```javascript try { const result = await items.update("collection-0.4576076907162254", { "_id": "6892593d-d57f-428d-bc53-3f0870a1beef", "_owner": "28962158-e9e0-4a9c-8bf0-5f654d78ccb6" }, { "appOptions": {}, "showDrafts": false, "suppressHooks": false }); return result; } catch (error) { console.error(error); throw error; } ```