Skip to content

Instantly share code, notes, and snippets.

@Able1991
Created June 10, 2020 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Able1991/8719d17dcb097af5848f79a8885fe69e to your computer and use it in GitHub Desktop.
Save Able1991/8719d17dcb097af5848f79a8885fe69e to your computer and use it in GitHub Desktop.
Batch update large collection with mongoose
schema.static("batchUpdate", async function batchUpdate(query, update) {
const batchSize = 100;
const first = await this.findOne(query, null, { sort: "_id" });
if (!first) {
return;
}
query.firstId = first.id;
const updateBatch = async () => {
if (!query.firstId) {
return;
}
const { firstId, ...updateQuery } = query;
const [last] = await this.find(
{
_id: { $gt: firstId },
...updateQuery,
},
null,
{
sort: "_id",
limit: 1,
skip: batchSize,
}
);
const lastId = last ? last.id : null;
if (lastId || firstId) {
updateQuery["_id"] = {};
if (firstId) {
updateQuery["_id"]["$gt"] = firstId;
}
if (lastId) {
updateQuery["_id"]["$lte"] = lastId;
}
}
await this.updateMany(updateQuery, update);
query.firstId = lastId;
await updateBatch();
};
return updateBatch();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment