Limit number of results in Meteor on the server side?

后端 未结 1 1041
灰色年华
灰色年华 2021-02-14 23:10

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

1条回答
  •  太阳男子
    2021-02-14 23:40

    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.

    0 讨论(0)
提交回复
热议问题