-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: Add forAll for cursor queries #9861
Comments
I have a few issues with this. First, finding a good name: let batch = [];
const batchSize = 1000;
await Users.find().eachAsync(async function(user) {
batch.push(user);
if (batch.length >= batchSize) {
await sendEmailToUsers(batch);
batch = [];
}
}); |
That's exactly what I did to solve it :D, but for me here's why I think it would be nice to have it as part of mongoose:
await Users
.find()
.batchSize(1000)
.eachBatch(async function (batch) {
await sendEmailToUsers(batch);
}); |
I think a |
@vkarpov15 Done I don't know exactly how you manage features but the feature branch was based on branch 6.0 and the PR is on branch 6.0 also I didn't want to come near the master branch as I'm assuming this is the branch used for the latest mongoose version |
feat: Add batch processing for eachAsync
Do you want to request a feature or report a bug?
Feature
Feature Description
For cursor queries (that return the data in batches) currently there is a method
forEach
which is used to loop over each document returned from the query not each batch. it would be nice to loop over each batch so you can perform a bulk action on each batch.Use Case
You want to send an email to all users currently in your system obviously you will fetch the whole user collection in batches. But it's not very efficient to send each user an email one at a time that wouldn't be very cost effective plus any mail service has a limit on the max number of bulk emails that could be sent (1000 for example). So the ideal situation would be to fetch the users in batches of 1000 and for each batch perform a bulk action of sending an email to all those users.
I can make the PR myself but I will wait for the issue approval
The text was updated successfully, but these errors were encountered: