How to use meteors collection.find () along with sorting, skipping and limiting
I am writing a chat program using Meteor. I need to show 10 latest posts in ascending order.
Messages.find({...}, {sort: {created: 1}, skip: getMessageCount()-10, limit: 10});
This code only shows me the first 10 posts.
Is the skip option in Meteor not working and I made a mistake, or are there any known bugs?
source to share
The reason it returns the first 10 messages is: {sort: {created: 1}}
which are returned in ascending order of the attribute value created
.
You have to write {sort: {created: -1}}
, i.e. records with a higher value created
will be returned first.
Also, assuming it created
contains a valid date value along with a timestamp, you must insert the value parsed
in to effectively sort by date.
source to share
You just need to generate on the server Messages.find({...}, {sort: {created: -1}, limit: 10});
AND then on the client Messages.find({...}, {sort: {created: 1}})
What happens here:
- You receive a splitting of the last 10 messages from Mongo, but in the wrong order
- You run the formatting of the desired order on the client
source to share