Skip to content

Instantly share code, notes, and snippets.

@jirevwe
Last active June 7, 2019 17:45
Show Gist options
  • Save jirevwe/bc3dd2e2ea85b2baa9169515b67b1b35 to your computer and use it in GitHub Desktop.
Save jirevwe/bc3dd2e2ea85b2baa9169515b67b1b35 to your computer and use it in GitHub Desktop.
Walks through the records in a mongoose collection using a number of cursors
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment