Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const start_time = performance.now();
- const count: number = 1000;
- const wallets: Wallet[] = [];
- const walletCount = await WalletModel.find({}).estimatedDocumentCount();
- // create a cursor for every `currentIndex` documents in the collection
- const getNthCursor = async (currentIndex: number) =>
- await WalletModel.find({ deleted_at: undefined }, { timeout: false })
- .lean()
- .limit(count)
- .skip(currentIndex * count)
- .sort({ balance: -1 })
- .cursor();
- // map the cursors to be passed to `Promise.all()`
- const cursorsPromises = [
- ...Array.from({ length: walletCount / count + 1 }).keys(),
- ].map(async (i: number) => await getNthCursor(i));
- const cursors = await Promise.all(cursorsPromises);
- const cursorMap = cursors.map(cursor =>
- cursor.eachAsync(wallet => wallets.push(wallet))
- );
- // execute cursors
- await Promise.all(cursorMap);
- const end_time = performance.now();
- const duration = (end_time - start_time) / 1000;
- console.log(duration)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement