猿问

如何执行批量更新插入

我正在尝试对文档执行批量更新插入。我知道这不能在本地完成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 中执行此操作的方法。


谢谢!


森栏
浏览 114回答 1
1回答

HUWWW

我最终使用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));
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答