【发布时间】:2017-04-14 20:16:18
【问题描述】:
我在 Meteor 中有两个集合 A 和 B。对于A,我有一个出版物,我在其中过滤掉了A 中的一系列文档。现在我想为B 创建一个出版物,我在B 中发布所有具有B.length 匹配A.length 字段的文档。
我找不到任何显示此内容的示例,但我认为它必须是标准用例。在 Meteor 中如何做到这一点?
【问题讨论】:
标签: meteor
我在 Meteor 中有两个集合 A 和 B。对于A,我有一个出版物,我在其中过滤掉了A 中的一系列文档。现在我想为B 创建一个出版物,我在B 中发布所有具有B.length 匹配A.length 字段的文档。
我找不到任何显示此内容的示例,但我认为它必须是标准用例。在 Meteor 中如何做到这一点?
【问题讨论】:
标签: meteor
这是reywood:publish-composite 的常见模式
从 'meteor/reywood:publish-composite' 导入 { publishComposite };
publishComposite('parentChild', {
const query = ... // your filter
find() {
return A.find(query, { sort: { score: -1 }, limit: 10 });
},
children: [
{
find(a) {
return B.find({length: a.length });
}
}
]
});
这是一个与serverTransform 完全不同的模式,因为在客户端上,您最终会得到两个集合 A 和 B,而不是合成单个集合 A,其中包含 B 的某些字段。后者更像是SQL 联接。
【讨论】:
Meteor.publishTransformed('pub', function() {
const filter = {};
return A.find(filter)
.serverTransform({
'B': function(doc) {
return B.find({
length: doc.length
}); //this will feed directly into miniMongo as if it was a seperate publication
}
})
});
【讨论】: