sort _id 的 mgo 翻译:-1

mgo translation of sort _id : -1

在经典的 mongodb 查询中,我将执行 :

.sort("_id":-1)

如何用 mgo 做?

err := C.Find(bson.M{"Receiver": userId}).Sort("_id":-1).All(&result)

不工作

问候和感谢

一系列排序操作可以简单地翻译成这样:

在MongoDB查询中:

.sort({"_id:" 1, "name": 1})

使用mgo:

err := C.Find(bson.M{"Receiver": userId}).Sort("_id", "name").All(&result)

如果任何排序操作需要以相反的顺序进行,您可以在 MongoDB 查询中使用 -1

.sort({"_id:" -1, "name": -1})

这被转换为 mgo 中字段名称前的简单​​ '-' 符号:

err := C.Find(bson.M{"Receiver": userId}).Sort("-_id", "-name").All(&result)

这记录在 Query.Sort():

func (q *Query) Sort(fields ...string) *Query

Sort asks the database to order returned documents according to the provided field names. A field name may be prefixed by - (minus) for it to be sorted in reverse order.