mgo Ordered Sort Aggregation

时间:2016-01-08 20:44:13

标签: mongodb go mgo

我有以下代码:

competitionMatch := bson.M{ "$match": bson.M{"competition" : bson.M{"$in" : []string{"PREMIERSHIP", "CHAMPIONSHIP", "LEAGUE1", "LEAGUE2"}}}}

group := bson.M{"$group" : bson.M{"_id" : "$homeTeam", "competitionOrder": bson.M{"$max": "$competitionOrder"}, "competition": bson.M{"$max" : "$competition"}}}
//sort := bson.M{"$sort" : bson.M{"competitionOrder": 1,"_id": 1}}
sort := bson.M{"$sort" : bson.D{{"competitionOrder", 1}, {"_id",1}}}
project := bson.M{"$project" : bson.M{"_id":1, "competitionOrder":1, "competition": 1}}

pipe := sessionCopy.DB("footballrecord").C(season).Pipe([]bson.M{competitionMatch, group, sort, project})

我正在尝试进行排序。评论sort可以正常工作,因为bson.M它是无序的,有时查询不会返回我期望的内容。

我正在尝试使用bson.D(未注释的行),但在运行查询时出现以下错误: the $sort key specification must be an object

知道我哪里出错了?

1 个答案:

答案 0 :(得分:0)

我发现这是godep的一个问题。导入语句没有被重写。