I have a piece of code that stores weather information every 30 seconds into a MongoDB collection. I figured out a way to publish the data for the last 24 hours from the server to the client, which goes as follows:
Server
Meteor.publish('mountCarmelData', function dataPublication(){
return MountCarmel.find({},
{
limit: 2880//max of 24 hours
});
});
Client
Meteor.subscribe('mountCarmelData');
If a new record is inserted and the total amount of records in the collection is less than the limit, this new record gets sent to the client automatically. The problem is that when there are more than 2880 records saved in the collection, the new records do not get sent to the client anymore.
I would like to know if there is a way to always send the most recent 2880 records to the client. Or maybe a way to just send the newly inserted record to the client side.
I need the last 24 hours of data to plot in a graph and I need the newly collected data (which is saved into the collection every 30 seconds) to dynamically update the weather variables.
you need to sort your collection at publish time. i assume you have some kind of time stamp on each record, you can sort by that. e.g.
{
limit: 2800,
sort: {createdAt: -1}
}
-1 will sort descending, newest to oldest.
note: this sort is for the publish, to ensure you're publishing the data you want to the client. if your client needs the data in a different order (i.e. descending is not the correct choice there), then the client can sort that published data however it needs to.