【问题标题】:How to Bulk update or Upsert in mongodb with nodejs?如何使用nodejs在mongodb中批量更新或Upsert?
【发布时间】:2018-11-30 12:04:16
【问题描述】:

我需要使用 mongoDB 批量更新方法更新集合中的 3000+ 条批量记录,但它一次插入单个记录。

我需要批量插入。请提出更好的方法

     function bulkGl(DataArray){
  var bulk = Transaction.collection.initializeUnorderedBulkOp();
  for(i in DataArray){
    if(typeof DataArray[i].convertedValue !== NaN){
      var Data =  { "currency_conversions":[{
        "converted_value" : DataArray[i].convertedValue
       }]};
       var query =  { testid : DataArray[i].testid};
       bulk.find(query).upsert().update( Data)
  console.log('statusdata',Data);
  }
}
bulk.execute(function(err,res){
  console.log(err,data);
});
}

【问题讨论】:

  • 一种方法可以是 1.获取满足您条件的所有文档 2.在 Js 数组中处理/更新这些文档 3.通过 insertMany 执行批量插入。当它进行批量插入时,它会覆盖该集合中存在的文档。

标签: node.js mongodb bulkupdate


【解决方案1】:

你应该使用updateMany()函数而不是update()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-05
    • 2021-01-07
    • 2020-07-19
    • 2011-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    相关资源
    最近更新 更多