我正在使用 ReactiveMongo,我想创建一个查询,该查询与 MongoDB 中的数字 (BigDecimal) 执行类似的查询。例如:像 4321.3456
这样的整数应该与 4321.34
匹配。
以下2个查询可在 MongoShell 上实现此目的:
db.employee.find({"$where":"/^4321.34.*/.test(this.salary)"})
db.collection.find({
"$where": function() {
return Math.round(this.salary * 100)/ 100 === 1.12;
}
})
但是,我找不到使用 ReactiveMongo 执行此查询的方法。
如何使用 ReactiveMongo 执行此类查询?
更新
我尝试过以下查询
val filter=Json.obj("$where" -> """/^4321.34.*/.test(this.salary)"""))
collection.find(filter).cursor[JsObject]()
最佳答案
就我而言,我确信我只会得到小数部分后的 2 位数字,因此我进行了范围查询,例如
val lowerLimit = 4321.34
val upperLimit = lowerLimit + 0.01
val filter = Json.obj("$gte" -> JsNumber(lowerLimit),"$lt" -> JsNumber(upperLimit)))
collection.find(filter).cursor[JsObject]()
仅当我们确定仅发送小数部分后两位数字时,上述查询才有效。如果发送小数部分后三位数字,我们必须执行 val upperLimit = lowerLimit + 0.001
。
关于mongodb - 如何在响应式 mongo 中查询 BigDecimal 的类似查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42111238/