Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor collection get previous and next item

Tags:

mongodb

meteor

I am trying to get the previous and next item in a collection. Below is what I have tried, but it is not working. I get results, but they are not returning in the correct order. Any suggestions?

Previous:

 Meteor.videos.find({$lt: currentID}, {sort: {date: -1}, limit:1});

Next:

Meteor.videos.find({$gt: currentID}, {sort: {date: -1}, limit:1});
like image 586
zero Avatar asked Aug 05 '14 02:08

zero


1 Answers

Try querying on the date, rather than document ID.

var current = Meteor.videos.findOne(currentID);

Previous:

Meteor.videos.find({date: {$lt: current.date}}, {sort: {date: -1}, limit:1});

Next:

Meteor.videos.find({date: {$gt: current.date}}, {sort: {date: 1}, limit:1});

You also need to sort the next cursor in ascending order.

like image 65
Neil Avatar answered Sep 27 '22 17:09

Neil