9

I have some code as shown below. There are about 60K entries in my keywords collection in Mongo. I really just want the top 25, so I'm trying to set a limit find. I'm running the same query to print out my number of results. For some reason, the limiting does not seem to work because my log message says 60K or so.

Is this sort of thing not possible? I can set a limit on the client side that works, but I thought I would try to limit things on the server so less data would get sent across.

A few bits of additional information:

  • I'm using my own Mongo database, not the one provided by meteor; pointing to it with the env variable
  • version 0.5.7

Any help would be greatly appreciated.

if (Meteor.isServer) {
   Meteor.startup(function() {
   console.log('server startup');

   Meteor.publish("keyword_hits", function() {
        console.log('keywords: ' + Keywords.find({}, {sort: {count:-1}, limit:25}).count());
        return Keywords.find({}, {sort: {count:-1}, limit:25});
    });
 });
Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
Eric Lee
  • 153
  • 1
  • 1
  • 4
  • 4
    Ah I think I found it, I was using the _id field myself. I changed my code to use the default and everything seems to work now. I'll remember to leave _id alone when using meteor. – Eric Lee Feb 26 '13 at 18:21
  • 6
    Hi Eric, Would you mind pasting the solution as an answer and closing the question? Or did you still need help with it? – cmather Mar 16 '13 at 02:06
  • One thing I notice is that you are sorting in your console.log statement. No reason to sort if you are only doing a count. – ChatGPT Nov 17 '14 at 03:01

1 Answers1

16

With the Mongo API that Meteor comes with, .count() does not take into consideration Limits, or Skips so what you're seeing is going to always be the max amount of things in your collection.

If you do

Meteor.publish("keyword_hits", function() {
        console.log('keywords: ' + Keywords.find({}, {sort: {count:-1}, limit:25}).fetch().length);
        return Keywords.find({}, {sort: {count:-1}, limit:25});
});

So instead of calling .count() and instead you call, .fetch() which returns the chosen 25 entries from your collection, then call .length on it, you will see the correct number of entries were returned.

I actually opened up an issue about .count() because in the normal Mongo you can do: .count({'applySkipLimit': true}); which will take into account, your skips and your limits.

Anyway, that's your problem.

Datsik
  • 14,453
  • 14
  • 80
  • 121