Search.../

bulkSave( )

Inserts or updates a number of items in a collection.

Description

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 to update the item in the collection, triggering the beforeUpdate() and afterUpdate() hooks for that item if they have been defined.
  • If no items in the collection have that _id value, or if the specified item does not have an _id property, bulkSave uses insert to add the specified item into the collection, triggering the beforeInsert() and 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.

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

Syntax

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

bulkSave Parameters

NAME
TYPE
DESCRIPTION
collectionId

string

The ID of the collection to save the item to.

items

Array<Object>

The items to insert or 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?

Save multiple items in a collection

Code Example

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5let toSave1 = {
6 "title": "Mr.",
7 "first_name": "John",
8 "last_name": "Doe"
9};
10
11let toSave2 = {
12 "title": "Ms.",
13 "first_name": "Jane",
14 "last_name": "Doe"
15};
16
17wixData.bulkSave("myCollection", [toSave1, toSave2])
18 .then( (results) => {
19 let inserted = results.inserted; // 2
20 let insertedIds = results.insertedItemIds; // see below
21 let updated = results.updated; // 0
22 let skipped = results.skipped; // 0
23 let errors = results.errors; // []
24 } )
25 .catch( (err) => {
26 let errorMsg = err;
27 } );
28
29/* insertedIds is:
30 *
31 * [
32 * "d9ea65a6-726a-4c3e-b97d-1128c0a06b5f",
33 * "472c1da9-e5e4-4620-8ee3-962e9d1a7df5"
34 * ]
35 */
Save multiple items with specified IDs in a collection

Code Example

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

Code Example

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5let toSave1 = {
6 "_id": "00001",
7 "title": "Mr.",
8 "first_name": "John",
9 "last_name": "Schmoe"
10};
11
12let toSave2 = {
13 "_id": "00003",
14 "title": "Prof.",
15 "first_name": "Jan",
16 "last_name": "Doey"
17};
18
19let options = {
20 "suppressAuth": true,
21 "suppressHooks": true
22};
23
24wixData.bulkSave("myCollection", [toSave1, toSave2], options)
25 .then( (results) => {
26 let inserted = results.inserted; // 1
27 let insertedIds = results.insertedItemIds; // ["00003"]
28 let updated = results.updated; // 1
29 let skipped = results.skipped; // 0
30 let errors = results.errors; // []
31 } )
32 .catch( (err) => {
33 let errorMsg = err;
34 } );
Save 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 toSave1 = {
8 "title": "Mr.",
9 "first_name": "John",
10 "last_name": "Doe",
11 "country": countryId
12};
13
14let toSave2 = {
15 "title": "Ms.",
16 "first_name": "Jane",
17 "last_name": "Doe",
18 "country": countryId
19};
20
21wixData.bulkSave("myCollection", [toSave1, toSave2])
22 .then( (results) => {
23 let inserted = results.inserted; // 2
24 let insertedIds = results.insertedItemIds; // see below
25 let updated = results.updated; // 0
26 let skipped = results.skipped; // 0
27 let errors = results.errors; // []
28 } )
29 .catch( (err) => {
30 let errorMsg = err;
31 } );
32
33/* insertedIds is:
34 *
35 * [
36 * "d9ea65a6-726a-4c3e-b97d-1128c0a06b5f",
37 * "472c1da9-e5e4-4620-8ee3-962e9d1a7df5"
38 * ]
39 */
Save multiple items including a reference to another item using the reference item

Code Example

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5let countryItem = // get country item from somewhere
6
7let toSave1 = {
8 "title": "Mr.",
9 "first_name": "John",
10 "last_name": "Doe",
11 "country": countryItem
12};
13
14let toSave2 = {
15 "title": "Ms.",
16 "first_name": "Jane",
17 "last_name": "Doe",
18 "country": countryItem
19};
20
21wixData.bulkSave("myCollection", [toSave1, toSave2])
22 .then( (results) => {
23 let inserted = results.inserted; // 2
24 let insertedIds = results.insertedItemIds; // see below
25 let updated = results.updated; // 0
26 let skipped = results.skipped; // 0
27 let errors = results.errors; // []
28 } )
29 .catch( (err) => {
30 let errorMsg = err;
31 } );
32
33/* insertedIds is:
34 *
35 * [
36 * "d9ea65a6-726a-4c3e-b97d-1128c0a06b5f",
37 * "472c1da9-e5e4-4620-8ee3-962e9d1a7df5"
38 * ]
39 */