so have mongo db query works fine:
db.levels.aggregate([{ $match: { "_id": {$lt: objectid("56410480f91e505237902dae")} }, }, { $group: { "_id": {"title": "level 11"}, "totalamount": { $sum: "$rewardcoins"} } } ])
it should rows before give id , based on rewardcoins calculate sum.
now mgo in golang i'm strugling working... i'm going pipe resultset empty.
pipe := c.pipe([]bson.m{{"$match": bson.m{"_id": bson.m{"$lt": level.id}}, "$group": bson.m{"_id":"$title", "totalreward": bson.m{"$sum": "$rewardcoins"}}}}) res :=[]bson.m{} pipe.all(&res)
what i'm doing wrong here ? thanks.
update
here how simple rows in db:
{ "_id" : objectid("5613f5ad153678d113d01f4a"), "title" : "level 1", "rewardcoins" : numberlong(1000) } { "_id" : objectid("56159796153678d113d02d60"), "title" : "level 2", "rewardcoins" : numberlong(50000) }
the structure in go isn't same in other language. if hit enter after every brace or comma, add commas go wants them, , run go fmt
, 1 bson.m
:
pipe := c.pipe( []bson.m{ { "$match": bson.m{ "_id": bson.m{ "$lt": level.id, }, }, "$group": bson.m{ "_id": "$title", "totalreward": bson.m{ "$sum": "$rewardcoins", }, }, }, }, )
another way see there's no }, {
before $group
in go, whereas there in json-y version.
indenting way seems idea in general; eyes can lost in sea of brackets otherwise.
Comments
Post a Comment