Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a published collection's total count, regardless of a specified limit, on the client?

Tags:

meteor

I'm using the meteor-paginated-subscription package in my app. On the server, my publication looks like this:

Meteor.publish("posts", function(limit) {
  return Posts.find({}, {
    limit: limit
  });
});

And on the client:

this.subscriptionHandle = Meteor.subscribeWithPagination("posts", 10);

Template.post_list.events = {
  'click #load_more': function(event, template) {
    template.subscriptionHandle.loadNextPage();
  }
};

This works well, but I'd like to hide the #load_more button if all the data is loaded on the client, using a helper like this:

Template.post_list.allPostsLoaded = function () {
  allPostsLoaded = Posts.find().count() <= this.subscriptionHandle.loaded();
  Session.set('allPostsLoaded', allPostsLoaded);
  return allPostsLoaded;
};

The problem is that Posts.find().count() is returning the number of documents loaded on the client, not the number available on the server.

I've looked through the Telescope project, which also uses the meteor-paginated-subscription package, and I see code that does what I want to do:

allPostsLoaded: function(){
  allPostsLoaded = this.fetch().length < this.loaded();
  Session.set('allPostsLoaded', allPostsLoaded);
  return allPostsLoaded;  
}

But I'm not sure if it's actually working. Porting their code into mine does not work.

Finally, it does look like Mongo supports what I want to do. The docs say that, by default, cursor.count() ignores the effects of limit.

Seems like all the pieces are there, but I'm having trouble putting them together.

like image 641
jrullmann Avatar asked Sep 11 '13 14:09

jrullmann


People also ask

How do you count the number of documents in a collection?

var value = db. collection. count(); and then print(value) or simply value , would give you the count of documents in the collection named collection .

Which option should be used to update all the documents with the specified condition in the MongoDB query?

The method can modify specific fields of an existing document or documents or replace an existing document entirely, depending on the update parameter. By default, the db.collection.update() method updates a single document. Include the option multi: true to update all documents that match the query criteria.

Which of the following command can be used to check the size of a collection named posts?

To view the statistics for a collection, including the data size, use the db. collection. stats() method from the mongo shell. Q 22 - Which of the following commands can cause the database to be locked?

How do I aggregate a collection in MongoDB?

To create an aggregation pipeline, you can use can use MongoDB's aggregate() method. This method uses a syntax that is fairly similar to the find() method used to query data in a collection, but aggregate() accepts one or more stage names as arguments.


1 Answers

None of the answers do what you really want becase none provide solution that is reactive.

This package does exactly what you want and also reactive.

publish-counts

like image 62
Mustafa Avatar answered Oct 04 '22 17:10

Mustafa