mgo有序排序聚合

huangapple go评论86阅读模式
英文:

mgo Ordered Sort Aggregation

问题

我有以下代码:

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

你有任何想法我做错了什么吗?

英文:

I have the following code:

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

I'm trying to perform a sort. The commented sort works but as it is bson.M it is unordered, sometimes the query is not returning what I expect.

I'm trying to use bson.D (the uncommented line) but I get the following error when the query is run:
the $sort key specification must be an object

Any idea where I'm going wrong?

答案1

得分: 0

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

英文:

I figured out this was an issue with godep. The import statements had not been rewritten.

huangapple
  • 本文由 发表于 2016年1月9日 04:44:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/34685745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定