繁体   English   中英

Mongodb $min 值有时大于 $max

[英]Mongodb $min value is greater than $max sometimes

我正在从 mongo 数据库中获取日期并返回$first$max$min$last ,但是发生了一些非常不寻常的事情 - $min大于$max 这是我的代码:

let Lowestdate = await ETHMongo.aggregate([
  {
    $match: { createdAt: { $gte: new Date(last), $lte: new Date(NEW) } },
  },    
  {
    $group: {
      _id: null,

      minFee_doc: { $first: "$$ROOT" },
      minFee: { $min: "$one" },

      firstFee: { $first: "$one" },
      lastFee: { $last: "$one" },
      maxFee: { $max: "$one" },
    },
  },
]).then((result) => {});

任何解决方案? 表格标题 错误的结果

看起来你的“数字”被保存为string ,在字符串比较中"99" > "1000000"

幸运的是,您可以很容易地解决这个问题,只需使用$toInt将字符串转换为数字即可

{
    $group: {
        _id: null,

        minFee_doc: { $first: "$$ROOT" },
        minFee: { $min: {$toInt: "$one"} },

        firstFee: { $first: "$one" },
        lastFee: { $last: "$one" },
        maxFee: { $max: {$toInt: "$one"} },
    },
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM