Bulk Insert Records into Database - NoSQL

This article discusses how you can insert multiple records of data into your NoSQL database. For this tutorial, we will be working with the MongoDB database. Using JS Code Block, data from the controls e.g. Tablegrid can be transformed upon which the bulk insert query syntax can be built and returned from the function.

Create Insert Query

Once you are done configuring the MongoDB connector, create an InsertMany query as shown below. Declare a variable in the Insert Field, in our case it is “records” which will take the bulk insert query as input passed by the JS Code Block.

App Builder

For this use case, we have taken a TableGrid binded to a MongoDB Collection and a button with the label “Insert Records”. The button’s click action will trigger the bulk insert logic.

Insert Button - Click Action

Add a JS Code Block and create variables for each Tablegrid field respectively.

Code

records = [];
for (let i = 0; i < ids.length; i++) {
  records.push({
    ID: ids[i],
    FirstName: firstnames[i],
    LastName: lastnames[i],
    Address: addresses[i],
    Email: emails[i],
    Designation: designations[i]
  });
}
output = records;

Code Explanation

An empty array “records” is created, and for each loop iteration a new object with all the appropriate key-value pairs is pushed onto the “records” array. Lastly, the value of “records” which is an array of objects is assigned to the output variable.

 
Now click on continue and add a variable that will hold the JSCode Block output, in our case, it is data.


 

Now add the database connector and select the Insert Query we created earlier. In the “records” field, select the JSCode output by clicking on the Use Keywords option.


 

Finally, run the app and select any of the rows from the TableGrid and click on the “Insert Records” button.


 

Seleted records from tablegrid appear in the MongoDB collection, once the “Insert Records” button is clicked.