I'm trying to use the insertMany method, but without success... I read this stackoverflow answer, but it does not tell how to do handle array of objects that are very big.
In my test case I have 9755 objects, with this script it imports all lines 9 times, which is not what I want...
I would like to dive the array in chunks of 1000 objects for each import.
How can I do this?
function bulkImportToMongo(arrayToImport, mongooseModel) {
const Model = require(`../../../models/${mongooseModel}`);
let counter = 0;
let temp = [];
arrayToImport.forEach(item => {
temp.push(item);
counter++;
if (counter % 1000 == 0) {
Model.insertMany(temp).then(mongoosedocs => {
console.log(`imported ${counter} objects`);
console.log(mongoosedocs.length);
temp = [];
});
}
});
}