Mongo database lag

Hello,

Update:

Hello,

For some reason instead of having a page with the set amount of limits my page keeps getting more and more elements shown (starts with 3, then more and more added, also can’t go back with prev).
Any idea why? Here’s the code I use:

Event handling

"click .nxtpage_over": function(event, template){
    Session.set('skip', (Session.get('skip') || 0) + 3);
  console.log("Going next page "+Session.get('skip')+" Limit is :"+Session.get('limit'));
  Meteor.subscribe('overtime', {skip:Session.get('skip'), limit:Session.get('limit')});
},
"click .prepage_over": function(event, template){
    Session.set('skip', (Session.get('skip') || 0) - 3);
    console.log("Going prev page "+Session.get('skip')+" Limit is :"+Session.get('limit'));
  Meteor.subscribe('overtime', {skip:Session.get('skip'), limit:Session.get('limit')});
 // Meteor.subscribe('overtime', {limit:Session.get('limit') || 3}, {skip:Session.get('skip') || 0});
},

HTML/Template

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

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);
}
});

Client-sided initial subscribe (declared just in main.js, it’s not inside a function)

Session.set('limit', 3);
Meteor.subscribe('overtime', {limit:Session.get('limit')});

Old post:

So, I’ve decided to stress test my application. Created a loop and added 10,000 entries. After entries been finally added I found out that the lag on my machine (client-side) is real. The only way to quick solve it was to add a server-sided limit.

Meteor.publish('trips', function(){
return Trips.find({}, {limit:100});
});

Is there a better way of doing this? Pagination perhaps?

Have you tried creating indexes on your Trips collection?

No, How do I do that?

Here’s how you can create index for the Trips collection on startup:

if (Meteor.isServer) {
Meteor.startup(function(){
// Create index
Trips.rawCollection().createIndex({ triplocation: -1 });
// DropIndex
Trips.rawCollection().dropIndex({ triplocation: -1 });
});
}

Replace ‘triplocation’ with your query parameter as deem fit.
You can also look here: https://forums.meteor.com/t/collection--ensureindex-creating-several-indexes/3324712

1 Like

That link doesn’t work sadly :frowning: . Nonetheless thank you very much. Will try that!

That is not the problem. It is lag because you publish entire data and manipulate at client-side. The best practice is that:

  • Publish needed data
  • Server-side paging
  • Server-side aggregation
    For example: You want to calculate the total number of products in your system. You should calculate at server side then send to client, do not publish to client and count at client-side.
    P/S: Index DB does not resolve the lagging problem at client side

If you make your publication like this, you can then change the options from the client :slight_smile:

Meteor.publish('trips', function(options){
  return Trips.find({}, options);
});

So then you could do something like this:

Template.myTemplate.onCreated(function(){
  Tracker.autorun(()=>{
    this.subscribe('trips', {limit:Session.get('limit') || 100, skip:Session.get('skip') || 0});
  });
});

Template.myTemplate.events({
  'click button#nextPage'(){
    Session.set('skip', (Session.get('skip') || 0) + 100);
  }
});

(quick and dirty example)

Or have a look at some atmosphere packages that can handle paginated subscriptions for you.

Adding and index won’t improve client performance at all btw. That’s only for the server database. The most performance heavy thing on the client is usually Blaze rendering btw, not the subscription/collection. Though if you have a big subscription it will take longer to send to the client.

3 Likes

Wow! That’s exactly what I needed/wanted. Thanks!!

Hello,

For some reason instead of having a page with the set amount of limits my page keeps getting more and more elements shown (starts with 3, then more and more added, also can’t go back with prev).
Any idea why? Here’s the code I use:

Event handling

"click .nxtpage_over": function(event, template){
    Session.set('skip', (Session.get('skip') || 0) + 3);
  console.log("Going next page "+Session.get('skip')+" Limit is :"+Session.get('limit'));
  Meteor.subscribe('overtime', {skip:Session.get('skip'), limit:Session.get('limit')});
},
"click .prepage_over": function(event, template){
    Session.set('skip', (Session.get('skip') || 0) - 3);
    console.log("Going prev page "+Session.get('skip')+" Limit is :"+Session.get('limit'));
  Meteor.subscribe('overtime', {skip:Session.get('skip'), limit:Session.get('limit')});
 // Meteor.subscribe('overtime', {limit:Session.get('limit') || 3}, {skip:Session.get('skip') || 0});
},

HTML/Template

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

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);
}
});

Client-sided initial subscribe (declared just in main.js, it’s not inside a function)

Session.set('limit', 3);
Meteor.subscribe('overtime', {limit:Session.get('limit')});