bulkUpdate( )
Updates a number of items in a collection.
Description
The bulkUpdate()
function returns a Promise that resolves after the items
have been updated in the specified collection. The Promise is rejected if the
current user does not have update permissions for the collection. Items are
skipped if they include an _id
property whose value does not match an
existing ID in the collection. Meaning, bulkUpdate()
cannot add new items
into the collection.
Calling the bulkUpdate()
function triggers the beforeUpdate()
and afterUpdate()
hooks for each item
that is updated if the hooks have been defined.
The bulkUpdate()
function compares the _id
property of the specified
items with the _id
property values of the items in the specified
collection.
Warning: If an existing item in the specified collection matches the
_id
of the specified item,bulkUpdate
replaces the existing 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 bulkUpdate()
function 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.
Use the options
parameter to run bulkUpdate()
from backend code without
checking for permissions or without its registered hooks.
When updating items in a collection that has a reference field, set
the values 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:
- Bulk operations are limited to 1000 items per function call.
- The
bulkUpdate()
function is not supported for Single Item Collections.
Syntax
function bulkUpdate(collectionId: string, items: Array<Object>, [options: WixDataOptions]): Promise<WixDataBulkResult>
bulkUpdate Parameters
NAME
TYPE
DESCRIPTION
The ID of the collection that contains the item to update.
To find your collectionId
, select the Databases tab in the Velo Sidebar.
Hover over your collection, click the three dots, and select Edit Settings.
The items to update.
The array must contain at least one item. Passing an empty array returns an error.
An object containing options to use when processing this operation.
Returns
Fulfilled - The results of the bulk save. Rejected - The error that caused the rejection.
Return Type:
NAME
TYPE
DESCRIPTION
The number of inserted items.
The number of updated items.
The number of skipped items.
List of IDs of inserted items.
List of IDs of updated items.
List of errors.
Was this helpful?
1import wixData from 'wix-data';23// ...45let toUpdate1 = {6 "_id": "00001",7 "title": "Mr.",8 "first_name": "John",9 "last_name": "Doe"10};1112let toUpdate2 = {13 "_id": "00002",14 "title": "Ms.",15 "first_name": "Jane",16 "last_name": "Doe"17};1819wixData.bulkUpdate("myCollection", [toUpdate1, toUpdate2])20 .then((results) => {21 let inserted = results.inserted; // 022 let insertedIds = results.insertedItemIds; // []23 let updated = results.updated; // 224 let skipped = results.skipped; // 025 let errors = results.errors; // []26 })27 .catch((err) => {28 let errorMsg = err;29 });
1import wixData from 'wix-data';23// ...45let toUpdate1 = {6 "_id": "00001",7 "title": "Mr.",8 "first_name": "John",9 "last_name": "Doe"10};1112let toUpdate2 = {13 "_id": "00002",14 "title": "Ms.",15 "first_name": "Jane",16 "last_name": "Doe"17};1819let options = {20 "suppressAuth": true,21 "suppressHooks": true22};2324wixData.bulkUpdate("myCollection", [toUpdate1, toUpdate2], options)25 .then((results) => {26 let inserted = results.inserted; // 027 let insertedIds = results.insertedItemIds; // []28 let updated = results.updated; // 229 let skipped = results.skipped; // 030 let errors = results.errors; // []31 })32 .catch((err) => {33 let errorMsg = err;34 });
1import wixData from 'wix-data';23// ...45let countryId = "id-of-usa-item";67let toUpdate1 = {8 "_id": "00001",9 "title": "Mr.",10 "first_name": "John",11 "last_name": "Doe",12 "country": countryId13};1415let toUpdate2 = {16 "_id": "00002",17 "title": "Ms.",18 "first_name": "Jane",19 "last_name": "Doe",20 "country": countryId21};2223wixData.bulkUpdate("myCollection", [toUpdate1, toUpdate2])24 .then((results) => {25 let inserted = results.inserted; // 026 let insertedIds = results.insertedItemIds; // []27 let updated = results.updated; // 228 let skipped = results.skipped; // 029 let errors = results.errors; // []30 })31 .catch((err) => {32 let errorMsg = err;33 });
1import wixData from 'wix-data';23// ...45let countryItem = // get country item from somewhere67let toUpdate1 = {8 "_id": "00001",9 "title": "Mr.",10 "first_name": "John",11 "last_name": "Doe",12 "country": countryItem13};1415let toUpdate2 = {16 "_id": "00002",17 "title": "Ms.",18 "first_name": "Jane",19 "last_name": "Doe",20 "country": countryItem21};2223wixData.bulkUpdate("myCollection", [toUpdate1, toUpdate2])24 .then((results) => {25 let inserted = results.inserted; // 026 let insertedIds = results.insertedItemIds; // []27 let updated = results.updated; // 228 let skipped = results.skipped; // 029 let errors = results.errors; // []30 })31 .catch((err) => {32 let errorMsg = err;33 });