2

How to get last 4 elements from meteor collection? For example. Posts = new Meteor.Collection('posts');

I like to get last 4 posts from "Posts" collections.

zevsuld
  • 276
  • 3
  • 15

1 Answers1

2

I assume that you have the date on the Posts items. You can do the following to get the "last post" or top 4 most recent:

Posts.find({}, {sort: {createdAt: -1}, limit: 4});

or if you mean the last or oldest Posts

Posts.find({}, {sort: {createdAt: 1}, limit: 4});

ralphie
  • 132
  • 1
  • 9
  • ralphie thank you! How can I get these posts one by one? For example if it was array i can get posts[post.length-1] is the last one, – zevsuld Sep 04 '14 at 01:50
  • please check my previous question asked here if it helps you. http://stackoverflow.com/questions/24729768/meteor-group-collection-by-field-and-their-sum/24730073?noredirect=1#comment38424845_24730073 – ralphie Sep 04 '14 at 08:57