MongoDb NORM - 分页和总文档

发布于 2024-11-19 07:22:14 字数 477 浏览 8 评论 0原文

使用 MongoDb NORM 驱动程序,有谁知道是否可以将光标定位到类似于下面的“查询”集合,以便可以检索文档的“页面”以及查询文档的总数?

> var j = db.People.find().skip(2).limit(2)
> j.count()
13
> j
{ "_id" : NumberLong(25), "Name" : "Ted" }
{ "_id" : NumberLong(26), "Name" : "Tom" }

因为我假设以下执行 MongoDbquery 两次......

totalItems = peopleCollection.Count(queryExpando);

peopleList = peopleCollection.Find(queryExpando, orderByExpando, pageSize, startIndex).ToList();

Using the MongoDb NORM driver, does anyone know whether it's possible to get the cursor to a 'queried' collection similar to below, so that a 'page' of documents can be retrieved as well as the total number of queried documents?

> var j = db.People.find().skip(2).limit(2)
> j.count()
13
> j
{ "_id" : NumberLong(25), "Name" : "Ted" }
{ "_id" : NumberLong(26), "Name" : "Tom" }

As I assume that the following performs the MongoDbquery twice...

totalItems = peopleCollection.Count(queryExpando);

peopleList = peopleCollection.Find(queryExpando, orderByExpando, pageSize, startIndex).ToList();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弥枳 2024-11-26 07:22:14

我不确定是否还有其他方法,但您可能可以使用 LINQ 来完成您需要的操作:

var allItems = peopleCollection.AsQueryable();
var count = allItems.Count();
var peopleList = allItems
                 .Where(p => p.Field == fieldValue)
                 .OrderBy(p => p.OrderByField)
                 .Skip(startIndex)
                 .Take(pageSize);

I am not sure if there is another way, but you can probably use LINQ to do what you need:

var allItems = peopleCollection.AsQueryable();
var count = allItems.Count();
var peopleList = allItems
                 .Where(p => p.Field == fieldValue)
                 .OrderBy(p => p.OrderByField)
                 .Skip(startIndex)
                 .Take(pageSize);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文