Last active
June 7, 2019 17:45
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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