如何在使用 mongodb $ 和 $lookup 条件将其应用于管道中的存储数据时修复 'Unrecognized expression $round'

How to fix 'Unrecognized expression $round' while applying it to stored data in pipeline using mongodb $and condition with $lookup

我在使用 Angular 7.
在浏览器中查看数据时对来自数据库的数据应用了精度 前端: Angular 7
后端: Java 和 Mongodb

前精度(以 db 为单位): 100.9999
用户可见精度后(小数点后 2): 101.00

UI 上有一个搜索功能,用户可以在其中输入任何金额范围。
场景 1:
用户输入t: 100 到 100
结果: 未找到记录(预期)

场景二:
用户输入: 101 到 101
结果:没有找到记录(根据user(101)应该找到了1条记录,但由于实际值为100.9999,所以不会return)

解决方案,我试图在应用查询中的搜索条件之前应用精度。
解前代码:

 "$pipeline": [{
        "$match": {
            "$expr": {
               "$and": [{
                          "$eq": ["$column1", "$valueForColumn1"]
                        },
                        {
                          "$gte": ["$amount", "$minValue"]
                        },
                        {
                          "$lte": ["$amount", "$maxValue"]
                        }]
                      }
                    }
                  }]

SQL等价于:

Select * from table_name where minVale >= ROUND(amount) <= maxValue;

我尝试了下面的代码,但它给出了错误。
应用解决方案后的代码:

  "$pipeline": [{
        "$match": {
            "$expr": {
               "$and": [{
                          "$eq": ["$column1", "$valueForColumn1"]
                        },
                        {
                          "$gte": [{"$round": ["$amount", 2]}, "$minValue"]
                        },
                        {
                          "$lte": [{"$round": ["$amount", 2]}, "$maxValue"]
                        }]
                      }
                    }
                  }]

在 $and 和 $gte/$lte 下使用“$round”时出现以下错误


Assert: command failed {
    ok: 0,
    errmsg: Unrecognised expression "$round"
    code: 168
    codeName: InvalidPipeline Operator

任何人都可以告诉我,我做错了什么以及我应该怎么做。

注意: 我也在查询中使用 $lookup,可能这就是它无法识别“$round”的原因。

我能看到的原因之一是您使用的 mongodb 版本不支持 $round 运算符。 $round 运算符是在 4.2 版及更高版本中引入的。 根据做Mongodb doc

升级您的 mongodb 版本并尝试。