Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor - Publish just the count for a collection

Is it possible to publish just the count for a collection to the user? I want to display the total count on the homepage, but not pass all the data to the user. This is what I tried but it does not work:

Meteor.publish('task-count', function () {
    return Tasks.find().count();
});

this.route('home', { 
    path: '/',
    waitOn: function () {
        return Meteor.subscribe('task-count');
    }
});

When I try this I get an endless loading animation.

like image 808
Jonathan Sutherland Avatar asked Jan 14 '15 16:01

Jonathan Sutherland


2 Answers

Meteor.publish functions should return cursors, but here you're returning directly a Number which is the total count of documents in your Tasks collection.

Counting documents in Meteor is a surprisingly more difficult task than it appears if you want to do it the proper way : using a solution both elegant and effective.

The package ros:publish-counts (a fork of tmeasday:publish-counts) provides accurate counts for small collections (100-1000) or "nearly accurate" counts for larger collections (tens of thousands) using the fastCount option.

You can use it this way :

// server-side publish (small collection)
Meteor.publish("tasks-count",function(){
  Counts.publish(this,"tasks-count",Tasks.find());
});

// server-side publish (large collection)
Meteor.publish("tasks-count",function(){
  Counts.publish(this,"tasks-count",Tasks.find(), {fastCount: true});
});

// client-side use
Template.myTemplate.helpers({
  tasksCount:function(){
    return Counts.get("tasks-count");
  }
});

You'll get a client-side reactive count as well as a server-side reasonably performant implementation.

This problem is discussed in a (paid) bullet proof Meteor lesson which is a recommended reading : https://bulletproofmeteor.com/

like image 68
saimeunt Avatar answered Nov 12 '22 06:11

saimeunt


I would use a Meteor.call

Client:

 var count; /// Global Client Variable

 Meteor.startup(function () {
    Meteor.call("count", function (error, result) {
      count = result;
    })
 });

return count in some helper

Server:

Meteor.methods({
   count: function () {
     return Tasks.find().count();
   }
})

*Note this solution would not be reactive. However if reactivity is desired it can be incorporated.

like image 28
Nate Avatar answered Nov 12 '22 08:11

Nate