Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. const start_time = performance.now();
  2. const count: number = 1000;
  3.  
  4. const wallets: Wallet[] = [];
  5. const walletCount = await WalletModel.find({}).estimatedDocumentCount();
  6.  
  7. // create a cursor for every `currentIndex` documents in the collection
  8. const getNthCursor = async (currentIndex: number) =>
  9. await WalletModel.find({ deleted_at: undefined }, { timeout: false })
  10. .lean()
  11. .limit(count)
  12. .skip(currentIndex * count)
  13. .sort({ balance: -1 })
  14. .cursor();
  15.  
  16. // map the cursors to be passed to `Promise.all()`
  17. const cursorsPromises = [
  18. ...Array.from({ length: walletCount / count + 1 }).keys(),
  19. ].map(async (i: number) => await getNthCursor(i));
  20.  
  21. const cursors = await Promise.all(cursorsPromises);
  22. const cursorMap = cursors.map(cursor =>
  23. cursor.eachAsync(wallet => wallets.push(wallet))
  24. );
  25.  
  26. // execute cursors
  27. await Promise.all(cursorMap);
  28.  
  29. const end_time = performance.now();
  30. const duration = (end_time - start_time) / 1000;
  31. console.log(duration)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement