按逻辑还是Mongodb聚合组? [英] Mongodb Aggregation Group by logical or?
问题描述
我想尝试基于文档中两个布尔字段的逻辑或进行分组.基本上,文档具有两个可以表示错误情况的字段,并且如果其中一个为true,则我想在id为true的情况下创建一个状态字段,但是如果两个都不为真,则我希望状态为false.我希望在分组中这样做,因为我需要分别汇总错误数据和成功条件数据.
I want to try and group based on a logical OR of two boolean fields in my documents. Basically the documents have two fields that can denote an error condition, and if either one is true, then I want to create a status field in the id of true, but if neither one is true, then I want the status to be false. I want this in the grouping because I need to aggregate data separately for errors than for success conditions.
我正在使用以下群组声明:
I'm working with the following group statement:
$group: {
_id: {
timestamp: {
$dateToString: {
format: "%Y-%m-%d %H:%M:%S.%LZ",
date: {
$subtract: ["$requestDtsCal", {
$mod: [{
$subtract: ["$requestDtsCal", baseDate]
}, divisor]
}]
}
}
},
serviceKey: "$serviceKey",
operationuuid: "$operationuuid",
contractKey: "$contractKey",
bindingTemplateKey: "$bindingTemplateKey",
containerKey: "$containerKey",
status: "$isSoapFaultByMP"
// status: { $or: [ {"$isSoapFaultByMP" : {$eq: true}}, {"$isSoapFaultByNextHop": {$eq: true}} ]}
},
如所写,它可以很好地识别"isSoapFaultByMP"字段为true或false.如果我尝试将带注释的变体与逻辑OR结合使用,则它将失败,并显示以下消息:
As written it aggregates fine identifying that the "isSoapFaultByMP" field is either true or false. if I try using the commented out variant with the logical OR, then it fails with this message:
assert: command failed: {
"ok" : 0,
"errmsg" : "invalid operator '$isSoapFaultByMP'",
"code" : 15999
}
关于我在做什么错的任何想法,或者甚至有可能这样做吗?这是Mongo 3.2.11 BTW.
Any thoughts on what I'm doing wrong, or if this is even possible? This is with Mongo 3.2.11 BTW.
谢谢
伊恩
推荐答案
您可以尝试一下吗,连同_id字段一起使用,无需检查布尔值是对还是错,只需对其进行评估
Can you try this, along with your _id fields, you don't need to check a boolean is true or false, just evaluate it
db.col.aggregate(
[
{
$group : { _id : {
isError : { $or : ["$isSoapFaultByMP", "$isSoapFaultByNextHop"] }
}
}
}
]
)
这篇关于按逻辑还是Mongodb聚合组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!