Search.../

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 200kb.

Note: Bulk operations are limited to 1000 items per function call.

Syntax

function bulkUpdate(collectionId: string, items: Array<Object>, [options: WixDataOptions]): Promise<WixDataBulkResult>

bulkUpdate Parameters

NAME
TYPE
DESCRIPTION
collectionId

string

The ID of the collection that contains the item to update.

items

Array<Object>

The items to update.

options
Optional

WixDataOptions

An object with one or both of the following boolean properties: suppressAuth, suppressHooks.

Returns

Fulfilled - The results of the bulk save. Rejected - The error that caused the rejection.

Return Type:

Promise<WixDataBulkResult>
NAME
TYPE
DESCRIPTION
inserted

number

The number of inserted items.

updated

number

The number of updated items.

skipped

number

The number of skipped items.

insertedItemIds

Array<string>

List of IDs of inserted items.

errors

Array<Error>

List of errors.

removed

number

The number of removed items.

removedItemIds

Array<string>

List of IDs of removed items.

Was this helpful?

Update multiple items with specified IDs in a collection

Code Example

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5let toUpdate1 = {
6 "_id": "00001",
7 "title": "Mr.",
8 "first_name": "John",
9 "last_name": "Doe"
10};
11
12let toUpdate2 = {
13 "_id": "00002",
14 "title": "Ms.",
15 "first_name": "Jane",
16 "last_name": "Doe"
17};
18
19wixData.bulkUpdate("myCollection", [toUpdate1, toUpdate2])
20 .then( (results) => {
21 let inserted = results.inserted; // 0
22 let insertedIds = results.insertedItemIds; // []
23 let updated = results.updated; // 2
24 let skipped = results.skipped; // 0
25 let errors = results.errors; // []
26 } )
27 .catch( (err) => {
28 let errorMsg = err;
29 } );
Update multiple items in a collection using data options

Code Example

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5let toUpdate1 = {
6 "_id": "00001",
7 "title": "Mr.",
8 "first_name": "John",
9 "last_name": "Doe"
10};
11
12let toUpdate2 = {
13 "_id": "00002",
14 "title": "Ms.",
15 "first_name": "Jane",
16 "last_name": "Doe"
17};
18
19let options = {
20 "suppressAuth": true,
21 "suppressHooks": true
22};
23
24wixData.bulkUpdate("myCollection", [toUpdate1, toUpdate2], options)
25 .then( (results) => {
26 let inserted = results.inserted; // 0
27 let insertedIds = results.insertedItemIds; // []
28 let updated = results.updated; // 2
29 let skipped = results.skipped; // 0
30 let errors = results.errors; // []
31 } )
32 .catch( (err) => {
33 let errorMsg = err;
34 } );
Update multiple items including a reference to another item using the referenced item's ID

Code Example

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5let countryId = "id-of-usa-item";
6
7let toUpdate1 = {
8 "_id": "00001",
9 "title": "Mr.",
10 "first_name": "John",
11 "last_name": "Doe",
12 "country": countryId
13};
14
15let toUpdate2 = {
16 "_id": "00002",
17 "title": "Ms.",
18 "first_name": "Jane",
19 "last_name": "Doe",
20 "country": countryId
21};
22
23wixData.bulkUpdate("myCollection", [toUpdate1, toUpdate2])
24 .then( (results) => {
25 let inserted = results.inserted; // 0
26 let insertedIds = results.insertedItemIds; // []
27 let updated = results.updated; // 2
28 let skipped = results.skipped; // 0
29 let errors = results.errors; // []
30 } )
31 .catch( (err) => {
32 let errorMsg = err;
33 } );
Update multiple items including a reference to another item using the referenced item

Code Example

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5let countryItem = // get country item from somewhere
6
7let toUpdate1 = {
8 "_id": "00001",
9 "title": "Mr.",
10 "first_name": "John",
11 "last_name": "Doe",
12 "country": countryItem
13};
14
15let toUpdate2 = {
16 "_id": "00002",
17 "title": "Ms.",
18 "first_name": "Jane",
19 "last_name": "Doe",
20 "country": countryItem
21};
22
23wixData.bulkUpdate("myCollection", [toUpdate1, toUpdate2])
24 .then( (results) => {
25 let inserted = results.inserted; // 0
26 let insertedIds = results.insertedItemIds; // []
27 let updated = results.updated; // 2
28 let skipped = results.skipped; // 0
29 let errors = results.errors; // []
30 } )
31 .catch( (err) => {
32 let errorMsg = err;
33 } );