Why take long time to count data

I have 60k+ data on my app, but if I execute Books.find().count() many times on google chrome console, the results are increasing until reached real total data,
so how to count data faster as I execute ‘db.getCollection(‘books’).find().count()’ on mongo console

my script: http://meteorpad.com/pad/rfoeDhdzepNubnnTe/Count%20Data

What you are seeing is minimongo synchronizing the data from the server to the client, which takes the longer the more documents you have. The size of the documents also matters.

For example, I have a collection with a lot of large documents in it. When I want my client to have access to that collection, I only sync the title field. When the client needs access to the entire document, it will get published it in it’s entirety.

You should probably also think about paging. In order to get the total number of books, you would need a method

 numberOfBooks: function() {
   return {ready: false, count: 0};
   return {ready: true, count: Books.find().count()};

that way you can call this method on the client, and it will immediately return {ready: false, count: 0} from the stub, and then the callback from the server will give you the true total.

Meteor.call('numberOfBooks', function(err, res) { ... }

where res will be {ready: .., count: ...}

1 Like

thanks jamgold, it’s solved my problem :smile: