在一个查询mongoose多更新

是否有任何方式来执行这个查询在mongoose?

这个多重更新是可能的从mongodb v2.6

{ update: <collection>, updates: [ { q: <query>, u: <update>, upsert: <boolean>, multi: <boolean> }, { q: <query>, u: <update>, upsert: <boolean>, multi: <boolean> }, { q: <query>, u: <update>, upsert: <boolean>, multi: <boolean> }, ... ], ordered: <boolean>, writeConcern: { <write concern> } } 

我发现这个话题,但它的相当古老: Mongodb多不同的值更新

thx每个人的build议

从您提供的详细信息中,我假定您想要根据几个不同的标准和每个特定查询的特定更新值发出一系列更新查询。

尽pipe如此,当涉及到更新MongoDB中的多个文档时,我将解决这两种可能的情况。

正如我前面提到的,如果您想更新多个文档,有两种可能的情况:

  • 更新多个匹配一组特定条件的文档,可以使用db.collection.update() ,通过在启动操作时指定multi参数官方的MongoDB Docs
  • 使用bulk.find.update()来链接几个multi update操作,并批量执行它们官方的MongoDB文档

好的,这些都是结果

 var p_text_data = require("../public/import/p_100_k_text_cz.json"); //data with new vals _und.map(p_text_data,function(vals,index) { var new_name = vals.name + something; e_textModel.collection.update({ 'id': vals.id }, { $set: { 'name': new_name } }, {upsert: false, multi: true}); }); 

使用sinle更新每个迭代,更新100k文件需要大约7+秒

和下一个代码,使用批量,less于3秒

 var bulk = e_textModel.collection.initializeUnorderedBulkOp(); _und.map(p_text_data,function(vals,index) { ite++; var new_name = vals.name + something; bulk.find( { 'id': vals.id } ).update( { $set: { 'name': new_name } } ); }); bulk.execute(); 

编辑:

 $var bulk = e_textModel.collection.initializeOrderedBulkOp(); 

在这种情况下甚至比UnorderedBulkOp快几倍