Using conditionals in mgo Pipe method?

385 views
Skip to first unread message

in...@jive.ly

unread,
Jun 2, 2014, 9:00:34 AM6/2/14
to mgo-...@googlegroups.com
I've been working with the Pipe() method successfully, however I would like to crate a conditional aggregate using $sum in the $group part of an aggregation framework query.

This is a snippet of the grouping section:

baseGroup := bson.M{
"$group": bson.M{
"_id": bson.M {},  // is filled separately - assume this works :-)
"Hits": bson.M{
"$sum": 1},

"Success": bson.M{
"$sum": bson.M{
"$cond": bson.M{
"if": bson.M{
"$Code": bson.M{"$eq": 200},
},
"then": 1,
"else": 0}}},

},
}


I am getting an emtpy result set with the "Success" section included (if I remove it, I get a dataset) - however I can't seem to figure out how to get the conditional method to work correctly - I've lifted the above pretty much from the mongo documentation (all fields are present, I've checked by summing the $Code field).

I have also tried reversing the if clause so that it is:

baseGroup := bson.M{
"$group": bson.M{
"_id": bson.M {},
"Hits": bson.M{
"$sum": 1},

"Success": bson.M{
"$sum": bson.M{
"$cond": bson.M{
"$Code": bson.M{
"if": bson.M{"$eq": 200},
},
"then": 1,
"else": 0}}},

},
}

So I don't think it's that. All documentation indicates that the $eq operator in this context takes an array - but I can;t seem to figure out how to pass an array into the query, so I'm not sure if that's what I am doing wrong...

Any ideas would be appreciated:-)

Thanks,
Martin

Putharekulu

unread,
May 8, 2017, 11:22:36 PM5/8/17
to mgo-users
Martin,

Just wondering were you able to find the solution to the problem ? I have same issue. I am trying to sum up a column value based on a condition using group.
Reply all
Reply to author
Forward
0 new messages