[英]How to perform Bulk Upsert
我正在尝试对文档执行批量 Upsert。 我知道这不能在Meteor:Mongo
本地完成,并且 Node MongoDB lib 必须与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
没有被设置。 看起来setOnInsert
不能作为updateOne
的选项。 什么是替代方案? 令人惊讶的是,我还没有在 Meteor 中找到一种方法来做到这一点。
谢谢!
我最终使用
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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.