Golang Bson在mgo中对参数进行排序 [英] Golang Bson sort parameters in mgo
问题描述
我试图将多重排序查询传递给mgo包的Sort参数(请参阅 https://godoc.org/labix.org/v2/mgo#Query.Sort )。
如果参数是动态的(现在保存在一个分片中),我怎样才能将它转换成有效的排序字符串。
一个工作示例是:
db.C(Collection).Find(Query).Limit(limit).Sort( - created_when,-title)。Iter()
但是如果-created_when和-title在切片中被保存,并且我尝试使用切片连接例如:
sortBy:= [] string { - created_when,title}
db.C Collection).Find(Query).Limit(limit).Sort(strings.Join(sortBy,,))。Iter()
查询无法正常工作。
如何将切片中的任意字段转换为.Sort([string1] ,[string2],...)格式?
db.C(Collection).Find (Query).Limit(limit).Sort(sortBy ...)。Iter()
I am trying to pass a multiple sort query to the "Sort" parameter of the mgo package (see https://godoc.org/labix.org/v2/mgo#Query.Sort).
If the parameters are dynamic (currently held in a slice), how can I translate that into a valid sort string.
A working example would be:
db.C(Collection).Find(Query).Limit(limit).Sort("-created_when", "-title").Iter()
But if "-created_when" and "-title" are held in a slice, and I try using a slice join like:
sortBy := []string{"-created_when", "title"}
db.C(Collection).Find(Query).Limit(limit).Sort(strings.Join(sortBy, ",")).Iter()
The query doesn't work correctly.
How can I translate the arbitrary fields in the slice into the .Sort([string1], [string2], ...) format required??
Like this:
db.C(Collection).Find(Query).Limit(limit).Sort(sortBy...).Iter()
这篇关于Golang Bson在mgo中对参数进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!