Mongo 批量更新 - 哪些成功(匹配和修改)哪些没有?

Mongo Bulk Updates - which succeeded (matched and modified) and which did not?

为了提高许多单个Mongo文档更新@Node的性能,我考虑使用Mongo的批量操作——每次迭代更新多达1000个文档。

在此用例中,每个单独的更新操作可能会或可能不会发生 - 仅当文档版本自更新程序上次读取后未发生更改时才会发生更新。如果文档未更新,应用程序需要重试 and/or 做其他事情来处理这种情况。

目前 Node 代码如下所示:

  col.update(
        {_id: someid, version:someversion},
        {$set:{stuf:toupdate, version:(someversion+1)}},
        function(err, res) {
           if (err) {   
              console.log('wow, something is seriously wrong!');
              // do something about it...

              return;
           }
           if (!res || !res.result || !res.result.nModified) { // no update
              console.log('oops, seems like someone updated doc before me);
              // do something about it...

              return; 
           }
           // Great! - Document was updated, continue as usual...
  });

使用Mongo的批量无序操作,有没有办法知道(1000)组更新中哪些更新成功,哪些未执行(在本例中是由于版本错误)?

我开始玩的代码如下所示:

var bulk = col.initializeUnorderedBulkOp();
bulk.find({_id: someid1, version:someversion1}).updateOne(
                 {$set:{stuf:toupdate1, version:(someversion1+1)}});
bulk.find({_id: someid2, version:someversion2}).updateOne(
                 {$set:{stuf:toupdate2, version:(someversion2+1)}});
...
bulk.find({_id: someid1000, version:someversion1000}).updateOne(
                 {$set:{stuf:toupdate1000, version:(someversion1000+1)}});
bulk.execute(function(err, result) {
           if (err) {   
              console.log('wow, something is seriously wrong!');
              // do something about it...

              return;
           }
           if (result.nMatched < 1000) { // not all got updated
              console.log('oops, seems like someone updated at least one doc before me);
              // But which of the 1000 got updated OK and which had not!!!!

              return; 
           }
           // Great! - All 1000 documents got updated, continue as usual...

});

我找不到 Mongo 解决方案。

我使用的解决方案是在批量操作失败时恢复到按文档操作...这在大多数情况下提供了合理的性能。