2

我正在尝试对文档执行批量 Upsert。我知道这不能在本地完成,Meteor:Mongo并且必须使用 Node MongoDB 库rawCollection

我有以下

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的 s 没有设置。看起来setOnInsert不能作为updateOne. 什么是替代方案?令人惊讶的是,我还没有找到在 Meteor 中执行此操作的方法。

谢谢!

4

1 回答 1

0

我最终使用

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));
于 2020-10-14T13:07:03.523 回答