Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use meteor limit properly

Tags:

limit

meteor

I would like to run a query in meteor and limit the number of field returned to only 5. Here is my code :

var courses =  Courses.find(
    {   day_of_week : {$in: day_selector}, 
        price : {$gt : price_min, $lt : price_max}, 
        starts : {$gt : schedule_min},
        ends : {$lt : schedule_max}}, 
            {limit : 10});
console.log(courses);
return courses;

However when I do this, I get all the courses that fit the selector in the console log and not only 10 of those. In the template everything is fine and only 10 courses are displayed.

I looked at this question : Limit number of results in Meteor on the server side?

but it did not help as I am not using specifics _id fields for my courses, I am using specific _id fields but for other collections though.

like image 582
ltbesh Avatar asked Oct 03 '13 13:10

ltbesh


1 Answers

Currently, the server is sending over your entire collection of courses and you are just filtering them to 10 on the client side. You can actually create a reactive subscription/publication to set the limit dynamically, or you could just limit the amount of records sent on the server

Meteor.publish('courses', function(limit) {
  //default limit if none set
  var dl = limit || 10;
  return Posts.find({}, {limit: dl});
});

Meteor.subscribe('courses', Session.get('limit'));

And then set the limit dynamically by using an event that calls:

Session.set('limit', 5);
like image 74
Pent Avatar answered Oct 04 '22 19:10

Pent