英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论