为了账号安全,请及时绑定邮箱和手机立即绑定

如何执行批量更新插入

如何执行批量更新插入

森栏 2023-05-11 16:03:19
我正在尝试对文档执行批量更新插入。我知道这不能在本地完成Meteor:Mongo,必须使用 Node MongoDB librawCollection代替。我有以下const bulkUpserts = [];for (let doc of docs) {  bulkUpserts.push({    updateOne: {      filter: {        fizz: doc.buzz      },      update: doc,      upsert: true,      setOnInsert: {        "foo": "bar",        "createdBy": Meteor.userId(),        "createdDate": new Date()      }    }  });}Collection.rawCollection().bulkWrite(bulkUpserts);^ 的问题是它setOnInsert似乎不起作用。我foo的没有设置。看起来setOnInsert不能作为updateOne. 有什么选择呢?令人惊讶的是,我还找不到在 Meteor 中执行此操作的方法。谢谢!
查看完整描述

1 回答

?
HUWWW

TA贡献1874条经验 获得超12个赞

我最终使用


const bulk = LineItems.rawCollection().initializeUnorderedBulkOp();


for (let doc of docs) {    

  bulk.find({

    fizz: doc.buzz

  }).upsert().updateOne({

    $set: doc,

    $setOnInsert: {

      _id: Random.id(),

      "foo": "bar",

      "createdBy": Meteor.userId(),

      "createdDate": new Date()

    }

  });

}

bulk.execute().then().catch(error => console.error(error));


查看完整回答
反对 回复 2023-05-11
  • 1 回答
  • 0 关注
  • 89 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信