简体   繁体   English

如何使用 MongoDB 和 javascript 管理批量更新脚本的错误?

[英]How to manage errors on bulk update script with MongoDB and javascript?

I currently have a script that update datas on a website using api calls.我目前有一个脚本可以使用 api 调用更新网站上的数据。 I loop through all the products in my database and I do my api calls to update the data on the website.我循环遍历数据库中的所有产品,然后拨打 api 电话来更新网站上的数据。

Sometimes an error occurs in the middle of the script execution so I'm not able to complete all the updates since I can't go through all the products in the database.有时在脚本执行过程中会发生错误,因此我无法完成所有更新,因为我无法 go 遍历数据库中的所有产品。 And when I restart my script it always start from the beginning (first products in the database) so I'm never completing my bulk update.当我重新启动我的脚本时,它总是从头开始(数据库中的第一个产品),所以我永远不会完成我的批量更新。

How can I continue updates after an error occurred?发生错误后如何继续更新? For example, if error occurred at the product number 10, how can I keep doing the updates for the product number 11 without restarting my script from the beginning?例如,如果产品编号 10 发生错误,我如何在不从头重新启动脚本的情况下继续对产品编号 11 进行更新? Is there a mongoDb function that tracks errors?有没有跟踪错误的mongoDb function?

I'm a beginner and I'm trying to understand the logics around bulk updates.我是初学者,我正在尝试了解有关批量更新的逻辑。 I've thought about saving the ID of the products where the error occurred and then start updating after this ID.我想过把出错的商品ID保存起来,然后在这个ID之后开始更新。 But, I'm sure there's a standard procedure for managing bulk updates error and failures.但是,我确信有一个标准程序来管理批量更新错误和失败。

I'm using javascript, nodeJS and cron for my script.我正在为我的脚本使用 javascript、nodeJS 和 cron。

When you bulkupdate, you receive errors in BulkWriteError and if the operation is ordered, it will be stopped executing.当您进行 bulkupdate 时,您会在BulkWriteError中收到错误,如果操作已被订购,它将停止执行。

First of all, divide your updates into some batches may be 1000 or less or more depending upon how many in total you have.首先,将您的更新分成一些批次,可能是1000或更少或更多,具体取决于您的总数。

Secondly, running the batch updates without order makes performance improvements as the next update doesn't wait for the current one to finish.其次,无序地运行批量更新可以提高性能,因为下一次更新不会等待当前更新完成。

db.collection.bulkWrite(
   [
      { insertOne : <document> },
      { updateOne : <document> },
      { updateMany : <document> },
   ],
   { ordered : false }
)

Thirdly, check for errors for the ids and push them to the next batch that you create or keep all the errors in one batch and run once you complete all the bulk updates.第三,检查 id 的错误并将它们推送到您创建的下一个批次,或者将所有错误保留在一个批次中,并在完成所有批量更新后运行。

We are following the same approach for more than 100k records and it works well today.我们对超过 10 万条记录采用相同的方法,并且在今天运行良好。 If you or anyone else have a better solution I would definitely hear it and implement it.如果您或其他任何人有更好的解决方案,我一定会听取并实施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM