如何“分类"?和“限制"结果在mongodb中? [英] How to "sort" and "limit" results in mongodb?

查看:30
本文介绍了如何“分类"?和“限制"结果在mongodb中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用"sort"和"limit"执行查询.使用 mgo ,您可以执行 Find(nil).Sort(-when").限制(10),但新的官方mongo驱动程序没有这样的方法.如何使用新驱动程序进行排序和限制"?

I'm trying to execute a query with "sort" and "limit". With mgo you could do Find(nil).Sort("-when").Limit(10) but the new, official mongo driver has no such methods. How can I sort and "limit" with the new driver?

推荐答案

官方驱动程序并不像 mgo 那样简单明了.您可以使用 findopt.Limit findopt.Sort 进行排序和限制.

The official driver is not straightforward as mgo. You can do sort and limit using the findopt.Limit and findopt.Sort.

您可以从官方存储库中查看示例.

You can see examples from the official repository.

https://github.com/mongodb/mongo-go-driver/blob/5fea1444e52844a15513c0d9490327b2bd89ed7c/mongo/crud_spec_test.go#L364

这篇关于如何“分类"?和“限制"结果在mongodb中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆