How to fetch next and previous item of the current one with Mongoose

后端 未结 2 1625
南方客
南方客 2020-12-15 11:25

I have a blog. On the individual post page I want to display a link to the previous, and if there is one, next post published in the bottom. The link should be the title of

2条回答
  •  無奈伤痛
    2020-12-15 11:55

    Find previous item:

    Post.findOne({_id: {$lt: curId}}).sort({_id: -1}).exec(cb)
    

    Find next item:

    Post.findOne({_id: {$gt: curId}}).sort({_id: 1}).exec(cb)
    

提交回复
热议问题