MongoDB:如何根据 ObjectID 更新 n 条记录 [英] MongoDB: how to update n records based on ObjectID

查看:52
本文介绍了MongoDB:如何根据 ObjectID 更新 n 条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含 100 万个文档的集合,我正在尝试根据 ObjectID 选择这些记录中的 50000 个,并更新其中一个值(我在 Mongo shell 中工作,在 Ubuntu 上运行).

I've created a Collection containing 1 million documents, and I'm trying to select 50000 of these records based on the ObjectID, and update one of the values (i'm working in Mongo shell, running on Ubuntu).

是否可以定义 50000 个文档范围?50000 范围内包含哪些文档并不重要,我只是想对一定数量的记录进行围栏并使用主 ID 运行更新操作,以便我可以测量性能时间.

Is it possible to define a 50000 document range? It doesn't matter which documents are included in the 50000 range, I simply want to ringfence a definite number of records and run an update operation using the primary id so that I can measure the performance time.

我尝试运行的代码不起作用:

The code I've tried to run doesn't work:

  use Assignment
  var _start = new Date()
  db.FlightsDate.update({$set:{Airtime: 8888}}).limit(50000).hint({_id:1});
  var _end = new Date();
  print("Time to Bulk Update AirTime key for 50000 documents… " + ((_end _start)/1000));

...我正在收集 MongoDB 需要我在命令中包含一个查询以指定要更新哪些文档(我现在从阅读其他帖子中了解到 .limit 不会限制记录数而不是.update 写入).

...i'm gathering that MongoDB needs me to include a query in the command to specify which docs are to be updated (I now understand from reading other posts that .limit won't constrain the number of records than an .update writes to).

请谁能建议一种方法,使我能够定义要更新的记录数?

Please can anyone advise a method that'll enable me to define the number of records to be updated?

感谢您的建议.

R,乔恩

推荐答案

如果您只是在寻找涵盖集合中 50,000 个文档的范围",那么您最好的方法是首先查询并找到您范围内的开始"和结束"文档.然后将该范围"规范应用于您的更新.

If you are simply looking for a "range" that covers 50,000 of the documents in the collection then your best approach is to query and find the "starting" and "ending" documents of your range first. Then apply that "range" specification to your update.

 var start_id = db.FlightsDate.find({}).limit(1).toArray()[0]._id;
 var end_id = db.FlightsDate.find({}).skip(49999).limit(1).toArray()[0]._id;

 var _start = new Date();
 db.FlightsDate.update(
     { "_id": { "$gte": start_id, "$lte": end_id } },
     { "$set"; { "Airtime": 8888 } },
     { "multi": true }
 );

 var _end = new Date();
 ( _end - _start )/1000;

如果您想再增加一个范围内的下一个 50,000,那么:

If you then wanted the next 50,000 in an additional range then :

 var start_id = db.FlightsDate.find(
     { "_id": { "$gt": end_id } }
 ).limit(1).toArray()[0]._id;

 var end_id = db.FlightsDate.find(
     { "_id": { "$gt": end_id } }
 ).skip(49999).limit(1).toArray()[0]._id;

然后再做一遍.

关键是您需要知道在一个范围内从何处开始"以及何时结束",以将您的更新限制为仅 50,000 个文档,而无需任何其他标准.

The point is you need to know where to "start" and when to "end" within a range to limit your update to just 50,000 documents without any other criteria to do so.

还要注意更新方法中multi"的用法.默认情况下,.update() 不会更新"超过 一个 文档,本质上是第一个匹配项.所以你的意思是更新范围内的所有文档",这就是为什么你需要在这里应用multi".

Also note the usage of "multi" in the update method there. By default, .update() does not "update" any more than one document, essentially being the first match. So what you mean to do is update "all documents in the range" and that is why you need to apply "multi" here.

这篇关于MongoDB:如何根据 ObjectID 更新 n 条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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