'moving save() outside of the loop to make it faster

I want to add documents to the collection using mongoose like this:

for(let c = 0; c < motherCards.length; c++) {
           
         ...

         const userCardModel = new db.UserCard(cloned);
         userCardModel.relatedModel = account;
         await userCardModel.save(); // for each iteration of the loop this saves the new document

}

As you see the await userCardModel.save(); is inside the loop and it causes very long time to save the all the documents

I wonder is there any working solution to make this faster?

For instance can we fill userCardModel somehow and move the await userCardModel.save(); outside of the loop?!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source