简体   繁体   English

MongoDB:如何基于ObjectID更新n条记录

[英]MongoDB: how to update n records based on ObjectID

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). 我创建了一个包含100万个文档的Collection,并且尝试基于ObjectID选择其中的50000条记录,并更新其中一个值(我在Mongo Shell中工作,在Ubuntu上运行)。

Is it possible to define a 50000 document range? 是否可以定义50000个文档范围? 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. 哪个文档包含在50000范围内都没有关系,我只想对一定数量的记录进行防护,并使用主ID运行更新操作,以便我可以测量性能时间。

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));

...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). ...我正在收集MongoDB需要我在命令中包含一个查询以指定要更新的文档(我现在从阅读其他文章中了解到.limit不会比.update写入限制记录数至)。

Please can anyone advise a method that'll enable me to define the number of records to be updated? 请问有人可以建议一种方法使我能够定义要更新的记录数吗?

Grateful for advice. 感谢您的建议。

R, Jon 乔恩

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. 如果您只是在寻找一个涵盖了集合中50,000个文档的“范围”,那么最好的方法是首先查询并找到您范围中“开始”和“结束”的文档。 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;

If you then wanted the next 50,000 in an additional range then : 如果您随后想要下一个50,000,请执行以下操作:

 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;

And do it all again. 再做一遍。

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. 关键是您需要知道某个范围内的“开始”和何时“结束”,以将更新限制为仅50,000个文档,而无其他条件。

Also note the usage of "multi" in the update method there. 还要注意那里的更新方法中“ multi”的用法。 By default, .update() does not "update" any more than one document, essentially being the first match. 默认情况下, .update()没有“更新”的任何一个以上文件,基本上是第一场比赛。 So what you mean to do is update "all documents in the range" and that is why you need to apply "multi" here. 因此,您的意思是更新“范围内的所有文档”,这就是为什么您需要在此处应用“多个”的原因。

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

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