Mongoose似乎不支持$max字段更新操作符,有什么建议吗?

Mongoose doesn't seem to support the $max field update operator, any advice?

本文关键字:什么 操作符 更新 不支持 字段 max Mongoose      更新时间:2023-09-26

我试图使用Mongoose在我的Node.js项目中使用$max更新操作符,但我认为a)我要么做了一些可怕的错误,要么b) Mongoose不支持此功能。(以这个为例):

var updateGameScores = function(game, newScore, callback) {
  models.Scores.update({
    gameName: game
  }, {
    $set: {
      $max: {
        highestScore: newScore
      }
    }
  }, {
    upsert: true
  }, function(err) {
    callback(err);
  });
};

给出以下错误:

MongoError: The dollar ($) prefixed field '$max' in '$max' is not valid for storage.

我在Mongoose文档中找不到关于这个(或类似的功能)的任何内容,这表明Mongoose不支持这个。如果确实是这样的话,是否有其他方法可以在最小化数据库操作数量的同时实现这一目标?

**: "如果指定值大于字段的当前值,则将字段的值更新为指定值"

考虑将更新表达式写成

{ $max: { highestScore: newScore } }

由于$max运算符已经隐含了一个$set .