如何在MongoDB中正确进行批量更新/更新

编程入门 行业动态 更新时间:2024-10-25 08:27:38
本文介绍了如何在MongoDB中正确进行批量更新/更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试:

  • 根据搜索条件查找文档,
  • 如果找到,请更新一些属性
  • 如果不插入具有某些属性的文档.

我正在使用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中正确进行批量更新/更新

本文发布于:2023-10-16 11:51:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497472.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:批量   正确   如何在   MongoDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!