如何正确使用流星极限 [英] How to use meteor limit properly

查看:94
本文介绍了如何正确使用流星极限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在流星中运行查询并将返回的字段数限制为5.这是我的代码:

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;

但是,当我这样做时,我会在控制台日志中找到所有适合选择器的课程,而不仅仅是其中的10门.在模板中,一切都很好,只显示10个课程.

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.

我看着这个问题: 在服务器端在Meteor中限制结果数吗?

但是它没有帮助,因为我没有在课程中使用特定的_id字段,而是在特定的_id字段中使用了其他集合.

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.

推荐答案

当前,服务器正在发送您的整个课程集合,而您只是在客户端将其过滤为10个课程.实际上,您可以创建一个反应性订阅/发布来动态设置限制,或者可以限制服务器上发送的记录数量

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);

这篇关于如何正确使用流星极限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆