如何使用猫鼬分页 [英] How to make pagination with mongoose

查看:107
本文介绍了如何使用猫鼬分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的集合中创建一个分页功能。如何找到具有开始和限制位置的文档,并在单个查询中获取总文档编号?

解决方案

您无法在一个查询中获取两个结果;你可以做的最好的事情就是使用一个Mongoose 查询 对象:

  var query = MyModel.find({}); 
query.count(function(err,count){...});
query.skip(5).limit(10).exec('find',function(err,items){...});

使用流控框架,如 async 如果需要,可以并行地执行它们。


I want to make a pagination feature in my Collection. How can find a documents with 'start' and 'limit' positions and get total document number in a single query?

解决方案

You can't get both results in one query; the best you can do is to get them both using one Mongoose Query object:

var query = MyModel.find({});
query.count(function(err, count) {...});
query.skip(5).limit(10).exec('find', function(err, items) {...});

Use a flow control framework like async to cleanly execute them in parallel if needed.

这篇关于如何使用猫鼬分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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