Last active
February 26, 2025 01:09
-
-
Save israelalagbe/40886ea41f4344a6842a9cce34b86167 to your computer and use it in GitHub Desktop.
Mongoose Get Decuments In Chunk (MongoDB)
This file contains hidden or 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 getUsersByChunkSize = async function * (chunkSize = 10) { | |
const limit = chunkSize; | |
let page = 1; | |
const total = await User.countDocuments({}); | |
for(let startAt = 0; startAt < total; startAt += limit) { | |
const users = await User.find({}).skip(startAt).limit(limit); | |
for(const user of users) { | |
yield user; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment