mpc75
mpc75

Reputation: 989

NodeJS | MongoDB | How do I slow down my inserts?

I have an array that is 8,000 items long and I'm creating a new document in my MongoDB Atlas database but I keep hitting the limit of 100 operations per second. I don't need the data to get in there lightning-fast (75 inserts per second is fine with me), so I tried sticking a Sleep function in the middle of the writing function, but it isn't operating as I expected.

What I expect and want my code to do:

  1. write the first document
  2. sleep for 20 milliseconds
  3. write the second document

What it seems to be doing now:

  1. write the first document
  2. immediately write the second document
  3. immediately write the third document

Here's an example of the code:

const Sleep = ms => new Promise(res => setTimeout(res, ms));

const MyFunction = array => {
  return Promise.all(array.map(row => {
    const item = new Item({
      "property1": row.property1,
      "property2": row.property2
    });
    Sleep(20) // tried putting a sleep here but that didn't work
    return item.save();
  }));
};

Upvotes: 1

Views: 174

Answers (1)

Ashish Modi
Ashish Modi

Reputation: 7770

You need to await your sleep method as it is async. But even then it will not work as it will just delay the creation of promise, not the execution.

Better to use batch-promises https://www.npmjs.com/package/batch-promises

const batch = require("batch-promises");

const MyFunction = async array => {
  return batch(100, array, row => {
    const item = new Item({
      "property1": row.property1,
      "property2": row.property2
    });
    return item.save();
  });
};

OR if you want to do sequentially

const MyFunction = async array => {
  for (const row of array) {
    const item = new Item({
      "property1": row.property1,
      "property2": row.property2
    });
    await item.save();
  }
};

Upvotes: 1

Related Questions