Meteor集合中的2个出版物良好实践

时间:2017-04-30 01:20:47

标签: javascript meteor

在我的集合中的一个Meteor.isServer函数中发布2个查找查询是好还是坏?

我有以下代码: deals.js / collection

namespace MVVM_SandBox.Models
{
    public class ItemModelBlah
    {
        public string Label { get; set; }
        public bool CoolStuff { get; set; }
    }
}

我想添加另一个类似的出版物:

Meteor.publish('deals', function () {
    return Deals.find({ userId: this.userId });
  });

第二次发布的原因是启用了仅显示该类结果的类别组件。

所以现在我可以在我的组件createContainer中订阅它。谢谢!

1 个答案:

答案 0 :(得分:2)

本同一个Collection中有超过1个出版物本身并没有错。

但是,在您的情况下,我不确定为Meteor出版物使用相同的'deals'标识符是个好主意。

如果您的出版物用于不同目的(通常在不同时间/不同组件中使用),则只需使用不同的名称/标识符。

但是,如果我理解正确,您实际上想要在同一个组件中使用它们,以便它接收来自当前用户或给定类别的Deals个文档?在这种情况下,只需使用带$or的MongoDB查询选择器:

Meteor.publish('deals', function () {
    return Deals.find({
        $or: [{
            userId: this.userId
        }, {
            category: 'technology'
        }]
    });
}

甚至返回一组游标:

Meteor.publish('deals', function () {
    return [
        Deals.find({ userId: this.userId }),
        Deals.find({ category: 'technology' })
    ];
}

(请注意,这也使您可以在单个出版物中发布来自不同集合的文档!)