Meteor:发布依赖于另一个游标字段

时间:2015-04-24 12:16:24

标签: mongodb meteor mongodb-query publish-subscribe

有人可以指出发布功能是否需要monogdb的forEach操作,流星' this.added或Meteor的observe功能。

当前的发布功能有效,但不确定这是否是我主要担心的反应。

对于添加到博客集合中的每个新文档,forEach函数是否都会运行?

user1 _id嵌入在Blogs集合中的多个文档中,如下所示:

creator: user1's_id

在此博客文档中,还有一个用于其他用户'协作者的字段:

creator: user1's_id
collaborator: anotherUser_id

User1可能是百博客文档的创建者,但协作者对于每个博客文档都将是不同的。

我想为creator的用户创建Meteor.publish函数并返回以下两个游标

  1. user1为creator
  2. 的所有博客文档
  3. 所有Meteor.users()都是collaborator

    Meteor.publish('creatorBlogContext', function() {
    
        var user = Meteor.users.findOne(this.userId)
    
        if (user && user.profile.userType === 'creator') {
            var blogsCursor =  Blogs.find({'creator': user._id}); //returns all blog docs the user has created.
    
            var userIdsArray = []
    
            Blogs.find({'creator': user._id}).forEach(function(doc) {
                userIdsArray.push(doc.collaborator)
            })
    
            var usersCursor = Meteor.users.find({_id: {$in: userIdsArray}})
    
            return [blogsCursor, usersCursor];
        }
    });
    

    当新的Blog文档因此forEach函数推出协作者的Meteor.user时,上述函数是否会被动地重新运行?

  4. 注意:我不希望使用铁路由器并在发布功能中执行findOne,具体取决于创建者正在查看的博客。我希望以核心流星方式做到这一点,所以请不要包装建议。

    谢谢,如果我不清楚,请询问进一步的信息。

0 个答案:

没有答案