Phirum
Phirum

Reputation: 705

how to find second last record from collection with meteor.js?

I would like to find 2nd last record form collection with Meteor. How can I get it. For example if I use SQL I will write query like this:

SELECT TOP 1 * From
(select Top 2 * from student ORDER BY Id DESC)                      
ORDER BY Id

How about in Meteor.

Thanks in advance.

Upvotes: 3

Views: 1284

Answers (1)

richsilv
richsilv

Reputation: 8013

Collection.find({}, {sort: {Field: -1}, limit: n}).fetch().pop();

Where:

  • Collection is your collection name.
  • Field is the field you're interested in.
  • You're looking for the nth last document.

Essentially, you're reverse-sorting by the given field, limiting the returned results to only the first n, and then popping off the last element in that result set (i.e. the nth).

Upvotes: 5

Related Questions