1

I'm trying to get a collection's count from the server to the client. I want to use it for paging and just so users will know about the number of documents available. It's important the count does update if documents are added or removed.

One problem is paging, where I'm limiting the amount of documents sent to the client with publish/subscribe. But in the case below, the client will not know if the MyPix collection does contain more than 4 documents:

Meteor.publish('MyPix', function(cursor) {
    return MyPix.find({}, {limit:4, skip:cursor});
})
Kai
  • 417
  • 4
  • 15
  • 2
    You should try using tmeasday;publish-counts : http://stackoverflow.com/questions/27948046/meteor-publish-just-the-count-for-a-collection/27948339#27948339 – saimeunt Jan 19 '15 at 11:22

1 Answers1

3

This is abit tricky,

a quick solution is to use this package, publish-counts

Server

Meteor.publish('publication', function() {
   Counts.publish(this, 'numberOfPosts', Posts.find());
   Counts.publish(this, 'numberOfUsers', Users.find());
});

Client

Meteor.subscribe('publication')

then to get numberofUsers or numberOfPosts

Counts.get('numberOfUsers') // returns numberOfUSers users
Mustafa
  • 1,738
  • 2
  • 24
  • 34