【问题标题】:How to render and show some of the blog post in one page and rest of them to another page in mongodb如何在 mongodb 的一个页面中呈现和显示一些博客文章,并将其余的博客文章显示到另一个页面
【发布时间】:2020-07-28 08:44:59
【问题描述】:

我使用 node js 和 mongodb 创建了博客站点。当我将博客呈现到其他页面时,所有博客都显示在一个页面中。但我不想在一页中显示所有帖子,我想在一页中显示 10 个帖子,其他到下一页。我想我可以使用 limit() 方法限制帖子。但我只能将它们限制为一页。但是我怎样才能将它们呈现到下一页。 示例我有 20 个帖子,我可以使用 limit (10) 方法将 10 个帖子呈现到一页。但是我怎样才能将剩下的 10 篇文章渲染并显示到下一页。如果有人弄清楚,请帮助我。

【问题讨论】:

    标签: node.js mongodb express


    【解决方案1】:

    你只需要在光标上调用skip

    请参阅cursor skip 上的 MongoDB 文档“这种方法可能对实现“分页”结果很有用。

    这个例子取自Mongodb的example

    function printStudents(pageNumber, nPerPage) {
      print( "Page: " + pageNumber );
      db.students.find()
                 .skip( pageNumber > 0 ? ( ( pageNumber - 1 ) * nPerPage ) : 0 )
                 .limit( nPerPage )
                 .forEach( student => {
                   print( student.name );
                 } );
    }
    

    【讨论】:

      猜你喜欢
      • 2019-05-17
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-15
      相关资源
      最近更新 更多