ilrein
ilrein

Reputation: 3923

Meteor/Mongo Return the Collection Item Before Current One

So, I have a super simple query:

return Quotes.findOne({}, {sort: {createdOn: -1}} );

Which returns the LAST quote.

I'd love to add a previous button, and for that, I'd love an elegant suggestion for how to modify the query to return prior to the last. Something like:

return Quotes.findOne({}, {sort: {createdOn: -1}}, { additionalQuery: secondLast } );

Any suggestions?

Upvotes: 1

Views: 44

Answers (1)

adeneo
adeneo

Reputation: 318322

I don't think there is a very elegant way of doing it, you'd have to find more documents and take the second to last out of that collection, for instance by getting two documents and skipping the first

return Quotes.find({}, {sort: {createdOn: -1}} ).limit(2).skip(1);

It seems findOne supports skipping documents at the beginning as well, which is probably just as good.

Upvotes: 2

Related Questions