> 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: bulkInsert(collectionId: string, items: Array, options: WixDataOptions) # Method package: wixData # Method menu location: wixData --> bulkInsert # Method Link: https://dev.wix.com/docs/velo/apis/wix-data/bulk-insert.md # Method Description: Adds a number of items to a collection. 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()`](wix-data.Hooks.html#beforeInsert) and [`afterInsert()`](wix-data.Hooks.html#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 an item in a collection, if the item doesn't have one or has one that is `undefined`. 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, the `createdDate` and `updatedDate` 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 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 `bulkInsert()` function is not supported for Single Item Collections. > - The `bulkInsert()` function does not support multi-reference fields. For multi-reference fields, use [`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 `bulkInsert()` 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. ## Insert multiple items into a collection ```javascript import wixData from 'wix-data'; // ... let toInsert1 = { "title": "Mr.", "first_name": "John", "last_name": "Doe" }; let toInsert2 = { "title": "Ms.", "first_name": "Jane", "last_name": "Doe" }; wixData.bulkInsert("myCollection", [toInsert1, toInsert2]) .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" * ] */ ``` ## Insert multiple items with specified IDs into a collection ```javascript import wixData from 'wix-data'; // ... let toInsert1 = { "_id": "00001", "title": "Mr.", "first_name": "John", "last_name": "Doe" }; let toInsert2 = { "_id": "00002", "title": "Ms.", "first_name": "Jane", "last_name": "Doe" }; wixData.bulkInsert("myCollection", [toInsert1, toInsert2]) .then((results) => { let inserted = results.inserted; // 2 let insertedIds = results.insertedItemIds; // ["00001", "00002"] let updated = results.updated; // 0 let skipped = results.skipped; // 0 let errors = results.errors; // [] }) .catch((err) => { let errorMsg = err; }); ``` ## Insert multiple items into a collection using data options ```javascript import wixData from 'wix-data'; // ... let toInsert1 = { "_id": "00001", "title": "Mr.", "first_name": "John", "last_name": "Doe" }; let toInsert2 = { "_id": "00002", "title": "Ms.", "first_name": "Jane", "last_name": "Doe" }; let options = { "suppressAuth": true, "suppressHooks": true }; wixData.bulkInsert("myCollection", [toInsert1, toInsert2], options) .then((results) => { let inserted = results.inserted; // 2 let insertedIds = results.insertedItemIds; // ["00001", "00002"] let updated = results.updated; // 0 let skipped = results.skipped; // 0 let errors = results.errors; // [] }) .catch((err) => { let errorMsg = err; }); ``` ## Insert 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 toInsert1 = { "title": "Mr.", "first_name": "John", "last_name": "Doe", "country": countryId }; let toInsert2 = { "title": "Ms.", "first_name": "Jane", "last_name": "Doe", "country": countryId }; wixData.bulkInsert("myCollection", [toInsert1, toInsert2]) .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" * ] */ ``` ## Insert 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 toInsert1 = { "title": "Mr.", "first_name": "John", "last_name": "Doe", "country": countryItem }; let toInsert2 = { "title": "Ms.", "first_name": "Jane", "last_name": "Doe", "country": countryItem }; wixData.bulkInsert("myCollection", [toInsert1, toInsert2]) .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" * ] */ ``` ---