Search code examples
mongodbmeteormethodscollectionstimer

Meteor - How to automatically remove a single item from a collection after a specific time period from within a server side method?


I wrote a click. event which calls a method. This method pushes single items (InfoId) into the collection called userManagement. So these items are assigned to that user.

eventhandler:

Template.available.events({
"click .push": function(e) {
    e.preventDefault();
    var InfoId = this.InfoId;
    Meteor.call('pushInfo', InfoId);
}, });

And the method:

Meteor.methods({
  'pushInfo': function(InfoId) {
    if (this.userId) {
      userManagement.update({
          '_id': this.userId
        }, {
          $push: {
            'activeInfos': InfoId
          }
        } 
      );
    }
  }
});

However, now I need to automatically remove exactly this previously added single item (InfoId) from 'activeInfos' after a specific time period e. g. three months.

Is there any way to do that?


Solution

  • for doing this you can use cronjob just install it using meteor add percolate:synced-cron

    in cron you need to do two things one is add a task to cron, second is start our cron.

    SyncedCron.add({
      name: 'your cron name',
      schedule: function(parser) {
        // parser is a later.parse object
        return parser.text('every 2 hours');
      },
      job: function() {
        console.log("hello");
      }
    });
    

    here schedule: is use to set time and in Job: we will add code which we want to run after a time we added in schedule.

    after this start your cron. for this add this

    SyncedCron.start();
    

    for more info check this link https://github.com/percolatestudio/meteor-synced-cron .

    for schedule timing read this http://bunkat.github.io/later/parsers.html#overview

    i hope this will help