0
我需要做一个简单的蒙戈查询它类似于这样的SQL查询与逻辑表达式比较字段值
Select * from insights where category = 1 and param_count > param_mean + 1
我需要做一个简单的蒙戈查询它类似于这样的SQL查询与逻辑表达式比较字段值
Select * from insights where category = 1 and param_count > param_mean + 1
理想情况下,你会使用$redact
作为此聚集的表情,加上初始$match
在至少可能使用索引用于非计算表达式:
db.collection.aggregate([
{ "$match": { "category": 1 } },
{ "$redact": {
"$cond": {
"if": { "$gt": [ "$param_count", { "$add": [ "$param_mean", 1 ] } ] },
"then": "$$KEEP",
"else": "$$PRUNE"
}
}}
])
如果你的MongoDB的“服务器”版本低于2.6没有$redact
运营商,那么你可以使用$where
后者评估JA vaScript表达为布尔值返回结果:
db.collection.find({
"category": 1,
"$where": "this.param_count > this.param_mean + 1"
})
而在语法更短,它需要相当多的处理时间,由于需要评估的JavaScript表达式哪家。
在可能的情况下,您应该使用$redact
,或者完全避免计算,而是将计算的评估值存储在文档中。最后的陈述对于“所有”数据库确实如此。