Let take this sample
export let index = (req: Request, res: Response) => {
for (let i = 0; i < 10; i++) {
let bulk = Faker.collection.initializeUnorderedBulkOp();
for (let y = 0; y < 200000; y++) {
bulk.insert({
name: randomName(),
nights: Math.random(),
price: Math.random(),
type1: Math.random(),
type2: Math.random(),
type3: Math.random(),
type4: Math.random(),
departure: mongoose.Types.ObjectId(randomAreaID()),
destination: mongoose.Types.ObjectId(randomAreaID()),
refundable: randomBool(),
active: randomBool(),
date_start: randomDate(),
date_end: randomDate(),
});
}
bulk.execute();
}
return res.json({data: true});
};
With this code I try to insert "some" documents to my collection.
I use initializeUnorderedBulkOp but If I try to save more than 1 million docs then I have a memory issue
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
I know that I can increase the memory but I want to find a better solution. Because now I need 2m records but in the future i will need 100m.
Any suggestion to avoid memory issues?