Why does count() not ignore limit and skip?


#1

I’m trying to get a total count of results for a query on a subscription that uses limit and skip to prevent sending every document from the collection to the client.

When I try to get a count() on a find() query for the collection however it returns only the count of items with maximum set by the limit. How do I get around this?

I’m trying to do this in a React container using the createContainer method.


#2

This has been a tough one for me, as the count has to be done on the server. In the end, I used this package with a block of code that looks like:

//server.js
Meteor.publish('contacts', function (query, options) {

 // auth stuff, match/check on the query and options
 
 let contacts = Contacts.find(query, options);
 ServerSession.set('contactCount', contacts.count(), this.connection.id);
 return contacts;

});

//client.js
Template.contactList.helpers({

  contactCount () {
    return ServerSession.get('contactCount');
  }

});