如何在使用mongodb $和$ lookup条件将其应用于管道中的存储数据时修复“无法识别的表达式$ round" [英] How to fix 'Unrecognized expression $round' while applying it to stored data in pipeline using mongodb $and condition with $lookup
问题描述
在使用Angular 7在浏览器中查看数据时,我已经对来自DB的数据应用了精度.
前端:Angular 7
后端:Java和Mongodb
I have applied precision to the data coming from DB, while viewing it in browser using Angular 7.
Front End: Angular 7
Back End: Java and Mongodb
精度前(以db为单位): 100.9999
对用户可见的精度(小数点后2位): 101.00
before precision (in db): 100.9999
after precision visible to user(2 after decimal): 101.00
UI上具有搜索功能,用户可以在其中输入任何金额范围.
方案1:
用户输入 t:100至100
结果:未找到任何记录(预期)
There is a search functionality on UI in which a user can write any amount range.
Scenario 1:
User input: 100 to 100
Result: no records found (expected)
方案2:
用户输入:101至101
结果:未找到记录(应根据用户(101)找到1条记录,但是由于实际值为100.9999,因此不会返回任何记录)
Scenario 2:
User input: 101 to 101
Result: no records found (should have found 1 record according to user(101), but since the actual value is 100.9999, it won't return any)
解决方案,我试图在甚至在查询中应用搜索条件之前就应用精度.
解决方案之前的代码:
Solution, I am trying to apply precision before even applying the search criteria in query.
Code before the solution:
"$pipeline": [{
"$match": {
"$expr": {
"$and": [{
"$eq": ["$column1", "$valueForColumn1"]
},
{
"$gte": ["$amount", "$minValue"]
},
{
"$lte": ["$amount", "$maxValue"]
}]
}
}
}]
SQL等效项是:
Select * from table_name where minVale >= ROUND(amount) <= maxValue;
我尝试了以下代码,但给出了错误.
应用解决方案后的代码:
I tried the following code, but it gives the error.
Code after applying the solution:
"$pipeline": [{
"$match": {
"$expr": {
"$and": [{
"$eq": ["$column1", "$valueForColumn1"]
},
{
"$gte": [{"$round": ["$amount", 2]}, "$minValue"]
},
{
"$lte": [{"$round": ["$amount", 2]}, "$maxValue"]
}]
}
}
}]
在$ and和$ gte/$ lte下使用"$ round"时,出现以下错误
I get the following error while using "$round" under $and and $gte/$lte
Assert: command failed {
ok: 0,
errmsg: Unrecognised expression "$round"
code: 168
codeName: InvalidPipeline Operator
任何人都可以告诉我,我在做什么错,我应该怎么做.
Can anyone tell, what am I doing wrong and how should I do it.
注意::我也在查询中使用$ lookup,可能就是为什么它无法识别"$ round".
NOTE: I am also using $lookup in the query, may be that is why it is not able to recognise "$round".
推荐答案
我看到的原因之一是您使用的mongodb版本不支持$ round运算符.在版本4.2和更高版本中引入了$ round运算符. 根据 Mongodb文档
One of the reason i can see is the version of mongodb you are using is not supporting $round operator. $round operator was introduced in version 4.2... and above. according to do Mongodb doc
升级您的mongodb版本并尝试.
Upgrade your mongodb version and try.
这篇关于如何在使用mongodb $和$ lookup条件将其应用于管道中的存储数据时修复“无法识别的表达式$ round"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!