Golang MongoDB按字母顺序排序,带有跳过和限制功能。

问题描述 投票:3回答:1

我需要将结果按字母顺序排序,每页限制为10个。但在我的代码中,我得到的结果是每页按字母顺序排列10个,下一个10也是从'a'开始。同样的... 我的代码是这样的

pageNo := 1
perPage := 10
DB.C("collection").Find(bson.M{"_id": bson.M{"$in": ids}}).Sort("name").Skip((pageNo - 1) * perPage).Limit(perPage).All(&results)

有什么办法可以先按字母顺序排序,然后再应用分页?

mongodb sorting go limit skip
1个回答
0
投票

这应该可以了!

filter := bson.M{"_id": bson.M{"$in": ids}}
skip := int64(0)
limit := int64(10)

opts := options.FindOptions{
  Skip: skip,
  Limit: limit
}

DB.C("collection").Find(nil, filter, &opts)
© www.soinside.com 2019 - 2024. All rights reserved.