我正在尝试:
- 根据搜索条件查找文档,
- 如果找到,请更新一些属性
- 如果不插入具有某些属性的文档.
我正在使用Bulk.unOrderedOperation,因为我也在执行单个插入操作.而且我想在一次操作中再次完成DB的所有操作.
I'm using a Bulk.unOrderedOperation as I'm also performing a single insert. And I want to do everything in one operation againast DB.
但是,由于什么原因,没有为更新/更新操作插入任何内容.
However something it's causing nothing is being inserted for the update/upsert operation.
这是插入文档:
var lineUpPointsRoundRecord = { lineupId: lineup.id, // String totalPoints: roundPoints, // Number teamId: lineup.team, // String teamName: home.team.name, // String userId: home.iduser, // String userName: home.user.name, // String round: lineup.matchDate.round, // Number date: new Date() }这是upsert文档:
This is the upsert document:
var lineUpPointsGeneralRecord = { teamId: lineup.team, // String teamName: home.team.name, // String userId: home.iduser, // String userName: home.user.name, // String round: 0, signupPoints: home.signupPoints, // String lfPoints: roundPoints+home.signupPoints, // Number roundPoints: [roundPoints] // Number };这就是我试图更新/更新的方式:
This is how I'm trying to upsert/update:
var batch = collection.initializeUnorderedBulkOp(); batch.insert(lineUpPointsRoundRecord); batch.find({team: lineUpPointsRoundRecord.teamId, round: 0}). upsert(). update({ $setOnInsert: lineUpPointsGeneralRecord, $inc: {lfPoints: roundPoints}, $push: {roundPoints: roundPoints} }); batch.execute(function (err, result) { return cb(err,result); });为什么不更新/更新?
那是使用水线ORM的JS代码,它也使用mongodb本机驱动程序.
That is JS code using waterline ORM which also uses mongodb native driver.
推荐答案您的语法基本上是正确的,但是您的常规执行是错误的,因此您应该将"upsert"操作与其他修改分开".否则,它们将发生冲突"并在发生"upsert"时产生错误:
Your syntax here is basically correct, but your general execution was wrong and you should have "seperated" the "upsert" action from the other modifications. These will otherwise "clash" and produce an error when an "upsert" occurs:
LineupPointsRecord.native(function (err,collection) { var bulk = collection.initializeOrderedBulkOp(); // Match and update only. Do not attempt upsert bulk.find({ "teamId": lineUpPointsGeneralRecord.teamId, "round": 0 }).updateOne({ "$inc": { "lfPoints": roundPoints }, "$push": { "roundPoints": roundPoints } }); // Attempt upsert with $setOnInsert only bulk.find({ "teamId": lineUpPointsGeneralRecord.teamId, "round": 0 }).upsert().updateOne({ "$setOnInsert": lineUpPointsGeneralRecord }); bulk.execute(function (err,updateResult) { sails.log.debug(err,updateResult); }); });确保您的 sails-mongo 是支持批量操作的最新版本.包含最近的节点本机驱动程序.最新的版本支持v2驱动程序.
Make sure your sails-mongo is a latest version supporting the Bulk operations properly be the inclusion of a recent node native driver. The most recent supports the v2 driver, which is fine for this.
更多推荐
如何在MongoDB中正确进行批量更新/更新
发布评论