bulkInsert( )
Adds a number of items to a collection.
Description
The bulkInsert()
function returns a Promise that resolves after the items
have been added to the specified collection.
The Promise is rejected if the current user does not have create permissions
for the collection. Items are skipped if they include an _id
property whose
value matches an existing ID in the collection. Meaning, bulkInsert()
cannot
overwrite an existing item in the collection.
Calling the bulkInsert()
function triggers the beforeInsert()
and afterInsert()
hooks for each item
that is inserted if the hooks have been defined.
Use the options
parameter to run bulkInsert()
from backend code without
checking for permissions or without its registered hooks.
When inserting items into 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 bulkInsert()
function adds the following properties and values to the item
when it adds it to the collection:
_id
: A unique identifier for the item with collection, if the item does not include one. You can optionally provide your own ID. Once an ID is assigned to an item it cannot be changed._createdDate
: The date the item was added to the collection._updatedDate
: The date the item was modified. When the item is first added, thecreatedDate
andupdatedDate
are the same.
Any valid JavaScript object can be added as a property value. The bulkInsert()
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.
The maximum size of an item that you can add to a collection is 200kb.
Note: Bulk operations are limited to 1000 items per function call.
Syntax
function bulkInsert(collectionId: string, items: Array<Object>, [options: WixDataOptions]): Promise<WixDataBulkResult>
bulkInsert Parameters
NAME
TYPE
DESCRIPTION
string
The ID of the collection to add the item to.
Array<Object>
The items to add.
WixDataOptions
An object with one or both of the following boolean properties: suppressAuth, suppressHooks.
Returns
Fulfilled - The results of the bulk insert. Rejected - The error that caused the rejection.
Return Type:
NAME
TYPE
DESCRIPTION
number
The number of inserted items.
number
The number of updated items.
number
The number of skipped items.
Array<string>
List of IDs of inserted items.
Array<Error>
List of errors.
number
The number of removed items.
Array<string>
List of IDs of removed items.
Was this helpful?
Code Example
1import wixData from 'wix-data';23// ...45let toInsert1 = {6 "title": "Mr.",7 "first_name": "John",8 "last_name": "Doe"9};1011let toInsert2 = {12 "title": "Ms.",13 "first_name": "Jane",14 "last_name": "Doe"15};1617wixData.bulkInsert("myCollection", [toInsert1, toInsert2])18 .then( (results) => {19 let inserted = results.inserted; // 220 let insertedIds = results.insertedItemIds; // see below21 let updated = results.updated; // 022 let skipped = results.skipped; // 023 let errors = results.errors; // []24 } )25 .catch( (err) => {26 let errorMsg = err;27 } );2829/* insertedIds is:30 *31 * [32 * "d9ea65a6-726a-4c3e-b97d-1128c0a06b5f",33 * "472c1da9-e5e4-4620-8ee3-962e9d1a7df5"34 * ]35 */
Code Example
1import wixData from 'wix-data';23// ...45let toInsert1 = {6 "_id": "00001",7 "title": "Mr.",8 "first_name": "John",9 "last_name": "Doe"10};1112let toInsert2 = {13 "_id": "00002",14 "title": "Ms.",15 "first_name": "Jane",16 "last_name": "Doe"17};1819wixData.bulkInsert("myCollection", [toInsert1, toInsert2])20 .then( (results) => {21 let inserted = results.inserted; // 222 let insertedIds = results.insertedItemIds; // ["00001", "00002"]23 let updated = results.updated; // 024 let skipped = results.skipped; // 025 let errors = results.errors; // []26 } )27 .catch( (err) => {28 let errorMsg = err;29 } );
Code Example
1import wixData from 'wix-data';23// ...45let toInsert1 = {6 "_id": "00001",7 "title": "Mr.",8 "first_name": "John",9 "last_name": "Doe"10};1112let toInsert2 = {13 "_id": "00002",14 "title": "Ms.",15 "first_name": "Jane",16 "last_name": "Doe"17};1819let options = {20 "suppressAuth": true,21 "suppressHooks": true22};2324wixData.bulkInsert("myCollection", [toInsert1, toInsert2], options)25 .then( (results) => {26 let inserted = results.inserted; // 227 let insertedIds = results.insertedItemIds; // ["00001", "00002"]28 let updated = results.updated; // 029 let skipped = results.skipped; // 030 let errors = results.errors; // []31 } )32 .catch( (err) => {33 let errorMsg = err;34 } );
Code Example
1import wixData from 'wix-data';23// ...45let countryId = "id-of-usa-item";67let toInsert1 = {8 "title": "Mr.",9 "first_name": "John",10 "last_name": "Doe",11 "country": countryId12};1314let toInsert2 = {15 "title": "Ms.",16 "first_name": "Jane",17 "last_name": "Doe",18 "country": countryId19};2021wixData.bulkInsert("myCollection", [toInsert1, toInsert2])22 .then( (results) => {23 let inserted = results.inserted; // 224 let insertedIds = results.insertedItemIds; // see below25 let updated = results.updated; // 026 let skipped = results.skipped; // 027 let errors = results.errors; // []28 } )29 .catch( (err) => {30 let errorMsg = err;31 } );3233/* insertedIds is:34 *35 * [36 * "d9ea65a6-726a-4c3e-b97d-1128c0a06b5f",37 * "472c1da9-e5e4-4620-8ee3-962e9d1a7df5"38 * ]39 */
Code Example
1import wixData from 'wix-data';23// ...45let countryItem = // get country item from somewhere67let toInsert1 = {8 "title": "Mr.",9 "first_name": "John",10 "last_name": "Doe",11 "country": countryItem12};1314let toInsert2 = {15 "title": "Ms.",16 "first_name": "Jane",17 "last_name": "Doe",18 "country": countryItem19};2021wixData.bulkInsert("myCollection", [toInsert1, toInsert2])22 .then( (results) => {23 let inserted = results.inserted; // 224 let insertedIds = results.insertedItemIds; // see below25 let updated = results.updated; // 026 let skipped = results.skipped; // 027 let errors = results.errors; // []28 } )29 .catch( (err) => {30 let errorMsg = err;31 } );3233/* insertedIds is:34 *35 * [36 * "d9ea65a6-726a-4c3e-b97d-1128c0a06b5f",37 * "472c1da9-e5e4-4620-8ee3-962e9d1a7df5"38 * ]39 */