omarosh2

script insert

Oct 1st, 2025
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const DUP_COUNT = 2000
  2. const BATCH_SIZE = 1000;
  3.  
  4. const doc = {
  5.   expires: ISODate("2025-10-01T12:08:20.153Z"),
  6.   session:
  7.     '{"cookie":{"originalMaxAge":157680000000,"expires":"2025-10-01T12:03:19.734Z","secure":true,"httpOnly":true,"path":"/","sameSite":true},"pairingUuid":"abdbe798-b3ad-4fbb-913c-6362484e8acf","terminalExternalId":"DigitalGoods","customerRelationUuid":"e0505e42-959d-4391-9697-51019edc0bc2","checkInDate":"2020-10-02T12:03:17.178Z","userIssuer":"twint-issuer1"}'
  8. };
  9.  
  10. let inserted = 0;
  11. while (inserted < DUP_COUNT) {
  12.   const n = Math.min(BATCH_SIZE, DUP_COUNT - inserted);
  13.  
  14.   const batch = [];
  15.   for (let i = 0; i < n; i++) {
  16.     batch.push(Object.assign({}, doc)); // shallow copy
  17.   }
  18.  
  19.   const res = db.sessions.insertMany(batch, { ordered: false });
  20.   inserted += Object.keys(res.insertedIds).length;
  21.  
  22.   // lightweight progress (every ~10 batches)
  23.   if ((inserted / BATCH_SIZE) % 10 === 0 || inserted === DUP_COUNT) {
  24.     print(`Inserted ${inserted} / ${DUP_COUNT}`);
  25.   }
  26. }
  27.  
  28. print(`Done. Inserted ${inserted} documents.`);
Advertisement
Add Comment
Please, Sign In to add comment