IanG
IanG

Reputation: 1549

Mongodb Aggregation Group by logical or?

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}} ]}
},

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
}

Any thoughts on what I'm doing wrong, or if this is even possible? This is with Mongo 3.2.11 BTW.

Thanks,

Ian

Upvotes: 2

Views: 893

Answers (1)

Saravana
Saravana

Reputation: 12817

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"] }
                }
            }
        }
    ]
)

Upvotes: 1

Related Questions