MongoDB-更新集合中所有记录的最快方法是什么? [英] MongoDB - what is the fastest way to update all records in a collection?

查看:37
本文介绍了MongoDB-更新集合中所有记录的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个收藏有900万条记录.我目前正在使用以下脚本来更新整个集合:

I have a collection with 9 million records. I am currently using the following script to update the entire collection:

simple_update.js

db.mydata.find().forEach(function(data) {
  db.mydata.update({_id:data._id},{$set:{pid:(2571 - data.Y + (data.X * 2572))}});
});

这是从命令行运行的,如下所示:

This is run from the command line as follows:

mongo my_test simple_update.js

所以我要做的就是根据简单的计算添加一个新字段 pid .

So all I am doing is adding a new field pid based upon a simple calculation.

有更快的方法吗?这需要花费大量时间.

Is there a faster way? This takes a significant amount of time.

推荐答案

您可以做两件事.

  1. 发送将"multi"标志设置为true的更新.
  2. 在服务器端存储功能,然后尝试使用服务器端代码执行.

该链接还包含以下建议:

That link also contains the following advice:

这是执行批管理工作的好技术.通过本地主机接口在服务器上运行mongo.这样,连接速度非常快,延迟很短.这比db.eval()更友好,因为db.eval()阻止了其他操作.

This is a good technique for performing batch administrative work. Run mongo on the server, connecting via the localhost interface. The connection is then very fast and low latency. This is friendlier than db.eval() as db.eval() blocks other operations.

这可能是最快的.您必须意识到,在单个服务器上发布9M更新将是一项繁重的工作.假设您每秒可以获得3k的更新,但您仍在谈论要运行近一个小时.

This is probably the fastest you'll get. You have to realize that issuing 9M updates on a single server is going to be a heavy operation. Let's say that you could get 3k updates / second, you're still talking about running for nearly an hour.

那并不是真正的"mongo问题",这将是硬件限制.

And that's not really a "mongo problem", that's going to be a hardware limitation.

这篇关于MongoDB-更新集合中所有记录的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆