发布一个 collection 和其他 collection 的文档,这些文档与第一个 collection 中的任何文档相关

Publish a collections and other collection's documents which have relation with any document in the first collection

场景是我想发布一个完整的collection和第一个collection中与任何文档相关的用户数据(例如个人资料)。
问题是如何发布那部分用户 collections?

嗯,有两种方法,第一种是使用包

https://atmospherejs.com/cottz/publish-with-relations

第二个 - 在发布功能中你可以 return 多个游标,来自 docs

Meteor.publish("roomAndMessages", function (roomId) {
  check(roomId, String);
  return [
    Rooms.find({_id: roomId}, {fields: {secretInfo: 0}}),
    Messages.find({roomId: roomId})
  ];
});

经过一些研究,我发现 reywood:publish-composite 完全解决了我的问题。
示例:

Meteor.publishComposite('getItemsList', {
find: function() {
    return Items.find({});
},
children: [
    {
        find: function(item) {
            return Meteor.users.find(
                { _id: item.userId },);
        }
    }
]});

这将发布所有项目文档以及与之相关的任何用户文档。 ( Items.userId 映射到 Meteor.users._id )