Mongo Skip & Limit query

Hello,
I’m trying to have a list of 4 items with pagination.
Server sided publish:

Meteor.publish('overtime', function(opt){
if (!Roles.userIsInRole(this.userId, 'admin')) {
return Overtime.find({userId: this.userId}, opt);
} else {
return Overtime.find({}, opt);
}
});

HTML List

  {{#each workover_small}}
  <tr>
        <td>{{day}}.{{month}}.{{year}}</td>
        <td>{{worknorm}} h</td>
        <td>{{workover}} h</td>
        <td>{{actnumb}}</td>
        <td>{{client}}</td>
        <td>{{transTime}} h</td>
        <td>{{dist}} KM</td>      
        <td>{{carNum}}</td>
        <td>{{odometer}} KM</td>
        <td>{{cost}}€</td>
        <td>{{transover}} h</td>
        <td>{{comment}}</td>
        <td><a style="text-decoration: inherit;color:inherit;cursor:pointer;" class="edit-overtemp"><span class="glyphicon glyphicon-pencil"></span></a></td>
        <td><a style="text-decoration: inherit;color:inherit;cursor:pointer;" class="delete-over"><span class="glyphicon glyphicon-trash"></span></a></td>
      </tr>

      {{/if}}

         {{#if selectedOEdit _id}} 
      <tr> 
      <td colspan="12">
      <input type="button" value="Cancel" style="width:49%;"  class="btn btn-info clOverEdit" > 
      <input type="submit" value="Save" style="width:49%;"   class="btn btn-info saveEditOTemp"> 
      </td>
      </tr>
      {{/if}}


    {{/each}}

The helper

workover_small: function(){

return Overtime.find({"userId" : {$eq: Meteor.userId()}}, {skip:Session.get('skip'), sort: {createdAt: -1}});
},

Subscription client-sided looks like

Session.setDefault('limit', 50);
Session.setDefault('skip', 0);
Meteor.subscribe('overtime', {skip:(Session.get('skip')||0), limit:(Session.get('limit')||50)});
Template.triphtml.onCreated(function(){ 
 var instance = this;
 Session.set('limit',50);
 instance.autorun(function(){
Meteor.subscribe('overtime', {skip:(Session.get('skip')||0), limit:(Session.get('limit')||50)});
});
});

Next and Previous buttons
JS


"click .nxtpage_over": function(event, template){
  Session.set('skip', (Session.get('skip') || 0) + 10);
  console.log("Skip: "+Session.get('skip')+" Limit: "+Session.get('limit'));
 
},
"click .prepage_over": function(event, template){
  Session.set('skip', (Session.get('skip') || 0) - 10);
//  if(Session.get('skip') < 0)
  //  Session.set('skip', 0);

   console.log("Skip: "+Session.get('skip')+" Limit: "+Session.get('limit'));


   //  Meteor.subscribe('overtime', { skip:(Session.get('skip')||0), limit:(Session.get('limit')||4),sort: {createdAt: -1}});
  },

HTML

      <button class="prepage_over"> Previous Page </button>
      <button class="nxtpage_over"> Next Page </button>

Results
Default (Limit 50, Skip 0)

If I press Next page nothing changes, with a lot of skips I get a huge chunk cut off. Why’s that? How to use skip & limit?

Hello there, this happens because you didn’t finish your previous subscriptions.

So, first you subscribe to 4 elements, after that 4 more elements, minimongo now has 8 elements subscribed to. Get it?

You can either close your previous subscription or apply your skip and limit options to your client query as well.

How do I close my previous subscription?

Instead of Meteor.subscribe you should use Template level subscription

Session.setDefault('limit',4);
Session.setDefault('skip',0);
Template.triphtml.onCreated(function(){
 var instance = this;
 Session.set('limit',4);
 instance.autorun(function(){
  instance.subscribe('overtime', {
   skip: Session.get('skip'), 
   limit: Session.get('limit'),
   sort: {createdAt: -1}
  });
 });
});

If I remove the skip part from the workover_small function I get interesting results like