如何正确使用meteor limit [英] How to use meteor limit properly

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

问题描述

我想在meteor中运行一个查询并将返回的字段数限制为只有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);

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

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