如何在 Node.js 中使用 Mongoose 进行分页?
我正在使用 Node.js 和 mongoose 编写一个 web 应用程序。如何对 .find()
调用获得的结果进行分页?我想要一个与 SQL 中的 "LIMIT 50,100"
相当的功能。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
我对这个问题中所接受的答案感到非常失望。这不会扩展。如果您阅读了cursor.skip()上的细则:
为了以可扩展的方式实现分页,将 limit() 与至少一个过滤条件结合起来,createdOn 日期适合多种用途。
I'm am very disappointed by the accepted answers in this question. This will not scale. If you read the fine print on cursor.skip( ):
To achieve pagination in a scaleable way combine a limit( ) along with at least one filter criterion, a createdOn date suits many purposes.
在根据 Rodolphe 提供的信息仔细研究 Mongoose API 后,我找到了这个解决方案:
After taking a closer look at the Mongoose API with the information provided by Rodolphe, I figured out this solution:
使用 mongoose、express 和 jade 进行分页 - 这里有一个链接我的博客有更多详细信息
Pagination using mongoose, express and jade - Here's a link to my blog with more detail
您可以像这样链接:
使用
exec
执行查询You can chain just like that:
Execute the query using
exec
在这种情况下,您可以将查询
page
和/或limit
作为查询字符串添加到您的 URL。例如:
?page=0&limit=25 // 这将被添加到您的 URL 中:http:localhost:5000?page=0&limit=25
因为它将是一个
String
我们需要将其转换为Number
进行计算。让我们使用 parseInt 方法来完成此操作,并提供一些默认值。顺便说一句
分页以
0
开始In this case, you can add the query
page
and/ orlimit
to your URL as a query string.For example:
?page=0&limit=25 // this would be added onto your URL: http:localhost:5000?page=0&limit=25
Since it would be a
String
we need to convert it to aNumber
for our calculations. Let's do it using theparseInt
method and let's also provide some default values.BTW
Pagination starts with
0
您可以使用一个名为 Mongoose Paginate 的小包,它会更容易。
在您的路线或控制器之后,只需添加:
You can use a little package called Mongoose Paginate that makes it easier.
After in your routes or controller, just add :
查询:
参数:
Query:
Params:
这是一个例子,你可以尝试一下,
This is a example you can try this,
尝试使用 mongoose 函数进行分页。限制是每页的记录数和页数。
Try using mongoose function for pagination. Limit is the number of records per page and number of the page.
简单而强大的分页解决方案
last_doc_id
:您获得的最后一个文档IDno_of_docs_required
:您要获取的文档数量,即5、10 、 50 等。last_doc_id
,您将获得 ie 5 个最新文档last_doc_id
然后你会得到接下来的 ie 5 个文档。Simple and powerful pagination solution
last_doc_id
: the last document id that you getno_of_docs_required
: the number of docs that you want to fetch i.e. 5, 10, 50 etc.last_doc_id
to the method, you'll get i.e. 5 latest docslast_doc_id
then you'll get the next i.e. 5 documents.这就是我在代码上所做的
这就是我所做的。
This is what I done it on code
That is how I done it.
有一些很好的答案给出了使用skip() & 的解决方案。 limit(),但是在某些场景下,我们还需要文档计数来生成分页。以下是我们在项目中所做的事情:
There are some good answers giving the solution that uses skip() & limit(), however, in some scenarios, we also need documents count to generate pagination. Here's what we do in our projects:
这是我附加到所有模型的版本。为了方便,它依赖于下划线,为了性能,它依赖于异步。 opts 允许使用 mongoose 语法进行字段选择和排序。
将其附加到您的模型架构中。
Here is a version that I attach to all my models. It depends on underscore for convenience and async for performance. The opts allows for field selection and sorting using the mongoose syntax.
Attach it to your model schema.
上面的答案很好。
Above answer's holds good.
您也可以使用以下代码行,
该代码将在最新版本的 mongo 中工作
you can use the following line of code as well
this code will work in latest version of mongo
实现此目的的可靠方法是使用查询字符串从前端传递值。假设我们想要获取 page #2 并将输出限制 25 个结果。
查询字符串将如下所示:
?page=2&limit=25 // 这将添加到您的 URL 中:http:localhost:5000?page=2&limit=25
让我们看看代码:
我建议将此逻辑实现到中间件中,以便您可以将其用于各种路由/控制器。
A solid approach to implement this would be to pass the values from the frontend using a query string. Let's say we want to get page #2 and also limit the output to 25 results.
The query string would look like this:
?page=2&limit=25 // this would be added onto your URL: http:localhost:5000?page=2&limit=25
Let's see the code:
I would suggest implementing this logic into middleware so you can be able to use it for various routes/ controllers.
您可以使用 mongoose-paginate-v2 来完成。欲了解更多信息点击此处
You can do using mongoose-paginate-v2. For more info click here
我找到了一种非常有效的方法并自己实现了它,我认为这种方法是最好的,原因如下:
唯一需要注意的是 Mongoose 的某些方法,例如
.save()
不能很好地处理精益查询,此类方法在此 很棒的博文,我真的推荐这个系列,因为它考虑了很多方面,例如类型安全性(防止严重错误)和 PUT/PATCH。我将提供一些上下文,这是一个 Pokémon 存储库,分页工作原理如下: API 从 Express 的
req.body
对象接收 unsafeId,我们需要将其转换为字符串以防止NoSQL 注入(它可能是带有邪恶过滤器的对象),这个 unsafeId 可以是空字符串或上一页最后一项的 ID,它是这样的:现在,要使用它并避免 Off-By-One 错误在前端,您可以按照以下方式进行操作,考虑到
pokemons
是从 API 返回的 Pokémons 文档数组:此处请注意,Mongoose ID 始终是连续的,这意味着任何较新的ID 总是大于旧的,这是这个答案的基础。
这种方法已经过 Off-By-One 错误测试,例如,页面的最后一个元素可以作为下一页的第一个元素(重复)返回,或者是上一页的最后一个元素和上一页的最后一个元素之间的元素。当前页面的第一页可能会消失。
当您完成所有页面并请求最后一个元素(不存在的元素)之后的页面时,响应将是一个带有 200(OK)的空数组,这太棒了!
I have found a very efficient way and implemented it myself, I think this way is the best for the following reasons:
The only caveat to this is that some methods of Mongoose, such as
.save()
will not work well with lean queries, such methods are listed in this awesome blog post, I really recommend this series, because it considers a lot of aspects, such as type security (which prevents critical errors) and PUT/ PATCH.I will provide some context, this is a Pokémon repository, the pagination works as the following: The API receives unsafeId from the
req.body
object of Express, we need to convert this to string in order to prevent NoSQL injections (it could be an object with evil filters), this unsafeId can be an empty string or the ID of the last item of the previous page, it goes like this:Now, to consume this and avoid Off-By-One errors in the frontend, you do it like the following, considering that
pokemons
is the Array of Pokémons documents that are returned from the API:As a note here, Mongoose IDs are always sequential, this means that any newer ID will always be greater than the older one, that is the foundation of this answer.
This approach has been tested agaisnt Off-By-One errors, for instance, the last element of a page could be returned as the first element of the following one (duplicated), or an element that is between the last of the previous page and the first of the current page might disappear.
When you are done with all the pages and request a page after the last element (one that does not exist), the response will be an empty array with 200 (OK), which is awesome!
有很多方法可以实现它,但我会使用两个
find()
aggregate()
但在
find()
函数中,我我两次访问数据库,但是使用aggregate()
,我们可以在一个查询中完成此操作现在,这取决于您的要求,您更喜欢哪一个。在大多数用例中,我更喜欢
aggregate()
而不是find()
,因为它提供了更多操作数据的工具。There are many ways to implement it, but I will go with two
find()
aggregate()
But here in
find()
function, I am hitting the database two times, but usingaggregate()
, we can do this in one queryNow, it is up to your requirement, which one you prefer. In most use cases, I prefer
aggregate()
instead offind()
as it gives more tools to manipulate data.最简单、更快捷的方法是使用 objectId 进行分页
例子;
初始加载条件
从响应数据中获取第一个和最后一个 ObjectId
页面下一个条件
页面下一个条件
在 mongoose 中
The easiest and more speedy way is, paginate with the objectId
Example;
Initial load condition
Take the first and last ObjectId from response data
Page next condition
Page next condition
In mongoose
最好的方法(IMO)是在有限的集合或文档内使用跳过和限制。
为了在有限的文档内进行查询,我们可以使用特定的索引,例如DATE类型字段上的索引。请参阅下面的内容
The best approach (IMO) is to use skip and limit BUT within a limited collections or documents.
To make the query within limited documents, we can use specific index like index on a DATE type field. See that below
最简单的分页插件。
https://www.npmjs.com/package/mongoose-paginate-v2
将插件添加到架构中,然后使用模型分页方法:
Most easiest plugin for pagination.
https://www.npmjs.com/package/mongoose-paginate-v2
Add plugin to a schema and then use model paginate method:
这是用于获取具有分页和限制选项的技能模型结果的示例函数
}
This is example function for getting the result of skills model with pagination and limit options
}
使用 ts-mongoose-pagination
Using ts-mongoose-pagination
MongoDB 官方博客有一个关于分页的条目,其中解释了为什么“跳过”可能会很慢并提供替代方案:https://www.mongodb.com/blog/post/paging-with-the-bucket-pattern--part-1
The MongoDB official blog has an entry about pagination, where they go through why 'skip' may be slow and offer alternatives: https://www.mongodb.com/blog/post/paging-with-the-bucket-pattern--part-1
下面的代码对我来说工作得很好。
您还可以在 countDocs 查询中添加查找过滤器和用户相同的内容以获得准确的结果。
Below Code Is Working Fine For Me.
You can add finding filters also and user same in countDocs query to get accurate results.
您可以像这样编写查询。
page :来自客户端的页码作为请求参数。
per_page :每页显示的结果数
如果您使用 MEAN 堆栈,以下博客文章提供了使用 Angular-UI Bootstrap 在前端创建分页并在后端使用 mongoose 跳过和限制方法的大量信息。
请参阅:https://techpituwa。 wordpress.com/2015/06/06/mean-js-pagination-with-angular-ui-bootstrap/
You can write query like this.
page : page number coming from client as request parameters.
per_page : no of results shown per page
If you are using MEAN stack following blog post provides much of the information to create pagination in front end using angular-UI bootstrap and using mongoose skip and limit methods in the backend.
see : https://techpituwa.wordpress.com/2015/06/06/mean-js-pagination-with-angular-ui-bootstrap/
您可以使用skip()和limit(),但效率非常低。更好的解决方案是对索引字段加上 limit() 进行排序。
我们 Wunderflats 在这里发布了一个小型库:https://github.com/wunderflats/goosepage
它使用的是第一种方式。
You can either use skip() and limit(), but it's very inefficient. A better solution would be a sort on indexed field plus limit().
We at Wunderflats have published a small lib here: https://github.com/wunderflats/goosepage
It uses the first way.