I've written nodejs script to read the JSON file and insert multiple records into mongo collection, I've millions of records and I don't want to insert all documents in one short. I would like to insert 300 documents per second and sleep for 30 sec and insert another 300 and so on? I'm new with NodeJS - can you please how can I achieve this with my below code? Appreciated your help and support.
app.js
const mongoClient = require("mongodb").MongoClient;
const util = require('util');
const fs = require('fs');
let database = null;
new mongoClient('mongodb://localhost:3000/', {
auth: {
user: 'admin',
password: 'password',
}
}).connect(
(err, db) => {
if (err) return console.error(err);
database = db.db('myDB');
fs.readFile('data.json', 'utf8', function(err, data) {
if (err) throw err;
var json = JSON.parse(data);
database.collection("test").insertMany(json, function(err, doc) {
console.log("Documents inserting");
if (err) throw err;
});
//db.close();
});
});
Sample Data: - I've millions of records like this in a single file.
[{
"firstName": "Ariel",
"lastName": "Bailey"
}, {
"firstName": "Lura",
"lastName": "Buckridge"
}, {
"firstName": "Milton",
"lastName": "Macejkovic"
}, {
"firstName": "Carolyn",
"lastName": "Hegmann"
}, {
"firstName": "Sid",
"lastName": "Beer"
}]