> 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: bulkSave(collectionId: string, items: Array, options: WixDataOptions) # Method package: wixData # Method menu location: wixData --> bulkSave # Method Link: https://dev.wix.com/docs/velo/apis/wix-data/bulk-save.md # Method Description: Inserts or updates a number of items in a collection. The `bulkSave()` function returns a Promise that resolves after the items have been added or updated in the specified collection. The Promise is rejected if the current user does not have the necessary permissions for the collection. The `bulkSave()` function inserts or updates the specified items, depending on whether they already exist in the collection. It compares the `_id` property value of the specified items with the `_id` property values of the items in the specified collection. + If an item in the collection has the specified `_id` value, `bulkSave()` uses [`update()`](#update) to update the item in the collection, triggering the [`beforeUpdate()`](wix-data.Hooks.html#beforeUpdate) and [`afterUpdate()`](wix-data.Hooks.html#afterUpdate) hooks for that item if they have been defined. + If none of the items in the collection contain that `_id` value, the specified item does not have an `_id` property, or if the specified item's `_id` property is `undefined`, `bulkSave()` uses [`insert()`](#insert) to add the specified item into the collection. This triggers the [`beforeInsert()`](wix-data.Hooks.html#beforeInsert) and [`afterInsert()`](wix-data.Hooks.html#afterInsert) hooks for that item if they have been defined. Use the `options` parameter to run `bulkSave()` from backend code without checking for permissions or without its registered hooks. When saving items to a collection that has a reference field, set the values of the reference fields to the referenced item's `_id` value or the entire referenced item object. The maximum size of an item that you can save to a collection is 500kb. > **Notes:** > - If an item's `_id` property value is set to `null` or an empty string, an error is thrown. > - Bulk operations are limited to 1000 items per function call. > - The `bulkSave()` function is not supported for Single Item Collections. > - The `bulkSave()` function does not support multi-reference fields. For multi-reference fields, use [`replaceReferences()`](https://www.wix.com/velo/reference/wix-data/replacereferences) or [`insertReference()`](https://www.wix.com/velo/reference/wix-data/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 `bulkSave()` will fail and issue an error. # Method Code Examples: *** Note: do not assume any prop names or enum values other than the ones in the example. ## Save multiple items in a collection ```javascript import wixData from 'wix-data'; // ... let toSave1 = { "title": "Mr.", "first_name": "John", "last_name": "Doe" }; let toSave2 = { "title": "Ms.", "first_name": "Jane", "last_name": "Doe" }; wixData.bulkSave("myCollection", [toSave1, toSave2]) .then((results) => { let inserted = results.inserted; // 2 let insertedIds = results.insertedItemIds; // see below let updated = results.updated; // 0 let skipped = results.skipped; // 0 let errors = results.errors; // [] }) .catch((err) => { let errorMsg = err; }); /* insertedIds is: * * [ * "d9ea65a6-726a-4c3e-b97d-1128c0a06b5f", * "472c1da9-e5e4-4620-8ee3-962e9d1a7df5" * ] */ ``` ## Save multiple items with specified IDs in a collection ```javascript import wixData from 'wix-data'; // ... let toSave1 = { "_id": "00001", "title": "Mr.", "first_name": "John", "last_name": "Schmoe" }; let toSave2 = { "_id": "00003", "title": "Prof.", "first_name": "Jan", "last_name": "Doey" }; wixData.bulkSave("myCollection", [toSave1, toSave2]) .then((results) => { let inserted = results.inserted; // 1 let insertedIds = results.insertedItemIds; // ["00003"] let updated = results.updated; // 1 let skipped = results.skipped; // 0 let errors = results.errors; // [] }) .catch((err) => { let errorMsg = err; }); ``` ## Save multiple items in a collection using data options ```javascript import wixData from 'wix-data'; // ... let toSave1 = { "_id": "00001", "title": "Mr.", "first_name": "John", "last_name": "Schmoe" }; let toSave2 = { "_id": "00003", "title": "Prof.", "first_name": "Jan", "last_name": "Doey" }; let options = { "suppressAuth": true, "suppressHooks": true }; wixData.bulkSave("myCollection", [toSave1, toSave2], options) .then((results) => { let inserted = results.inserted; // 1 let insertedIds = results.insertedItemIds; // ["00003"] let updated = results.updated; // 1 let skipped = results.skipped; // 0 let errors = results.errors; // [] }) .catch((err) => { let errorMsg = err; }); ``` ## Save multiple items including a reference to another item using the referenced item's ID ```javascript import wixData from 'wix-data'; // ... let countryId = "id-of-usa-item"; let toSave1 = { "title": "Mr.", "first_name": "John", "last_name": "Doe", "country": countryId }; let toSave2 = { "title": "Ms.", "first_name": "Jane", "last_name": "Doe", "country": countryId }; wixData.bulkSave("myCollection", [toSave1, toSave2]) .then((results) => { let inserted = results.inserted; // 2 let insertedIds = results.insertedItemIds; // see below let updated = results.updated; // 0 let skipped = results.skipped; // 0 let errors = results.errors; // [] }) .catch((err) => { let errorMsg = err; }); /* insertedIds is: * * [ * "d9ea65a6-726a-4c3e-b97d-1128c0a06b5f", * "472c1da9-e5e4-4620-8ee3-962e9d1a7df5" * ] */ ``` ## Save multiple items including a reference to another item using the reference item ```javascript import wixData from 'wix-data'; // ... let countryItem = // get country item from somewhere let toSave1 = { "title": "Mr.", "first_name": "John", "last_name": "Doe", "country": countryItem }; let toSave2 = { "title": "Ms.", "first_name": "Jane", "last_name": "Doe", "country": countryItem }; wixData.bulkSave("myCollection", [toSave1, toSave2]) .then((results) => { let inserted = results.inserted; // 2 let insertedIds = results.insertedItemIds; // see below let updated = results.updated; // 0 let skipped = results.skipped; // 0 let errors = results.errors; // [] }) .catch((err) => { let errorMsg = err; }); /* insertedIds is: * * [ * "d9ea65a6-726a-4c3e-b97d-1128c0a06b5f", * "472c1da9-e5e4-4620-8ee3-962e9d1a7df5" * ] */ ``` ---