Skip to content

Instantly share code, notes, and snippets.

@israelalagbe
Last active February 26, 2025 01:09
Show Gist options
  • Save israelalagbe/40886ea41f4344a6842a9cce34b86167 to your computer and use it in GitHub Desktop.
Save israelalagbe/40886ea41f4344a6842a9cce34b86167 to your computer and use it in GitHub Desktop.
Mongoose Get Decuments In Chunk (MongoDB)
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