Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSS

insert millions of records mongodb

import { MongoClient } from 'mongodb';
const url = 'mongodb://mongo:27017';

const client = new MongoClient(url);

const test = async (start, end) => {
  const array = [...Array(end - start + 1).keys()].map(x => x + start);
  const obj = array.map(id => {
    return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(cid => {
      return { id, cid, pid: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] }
    });
  });
  await client.connect();

  await client.db('test2').collection('test').insertMany(obj.flat());
}

const testAsCunck = async (chunk) => {
  for (let inx = 0; inx < 1000; inx++) {
    console.log(inx*chunk+1, (inx*chunk+chunk));
    const start = inx*chunk+1;
    const end = inx*chunk+chunk;
    await test(start, end);
  }
}

//insert 10million records 
testAsCunck(10000);
 
PREVIOUS NEXT
Tagged: #insert #millions #records #mongodb
ADD COMMENT
Topic
Name
2+4 =