给定MongoDB集合中的以下文档...
{ "_id": 1, "amount": { "value": 1.123456789999, "rate": 1.2 }}
{ "_id": 2, "amount": { "value": 2.9844, "rate": 1.2 }}
{ "_id": 3, "amount": { "value": 1.123876, "rate": 1.2 }}
{ "_id": 4, "amount": { "value": 3.3557886, "rate": 1.2 }}
{ "_id": 5, "amount": { "value": 1.12599976, "rate": 1.2 }}
...可以选择所有那些文档amount
是1.12(即在查询中获得2位小数精度)的文档吗?
您可以与$where
操作员进行此操作。
db.collection.find({ "$where": function() {
return Math.round(this.amount.value * 100)/ 100 === 1.12;
}
})
编辑(在此评论之后)
在这种情况下,您应该使用聚合框架,尤其是$redact
运算符,这比使用以下解决方案要快得多$where
db.collection.aggregate([
{ "$redact": {
"$cond": [
{ "$eq": [
{ "$subtract": [
"$amount.value",
{ "$mod": [ "$amount.value", 0.01 ] }
]},
0.03
]},
"$$KEEP",
"$$PRUNE"
]
}}
])
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句