【问题标题】:Meteor - Publish a collection based on the users statusMeteor - 根据用户状态发布集合
【发布时间】:2016-02-26 08:22:33
【问题描述】:

我正在尝试为拥有accountStatus.isUserAccountActive: true 的用户发布类集合。我当前的代码发布了活跃的用户,然后发布了整个类集合。有人可以告诉我我做错了什么吗?

Meteor.publish('classList', function (group) {
  if (Roles.userIsInRole(this.userId, ['is_teacher'], group)) {
    return [
    Meteor.users.find({roles:'is_student', "accountStatus.isUserAccountActive": true}, 
      {fields: {
        "profile.firstName": 1, 
        "profile.familyName": 1,
        roles: 1, 
        "accountStatus.isUserAccountActive": 1
      }
    }),
    classes.find({teacherUserId: this.userId},
      {fields: {
        "studentUserId": 1,
        "class": 1
      }})
    ];
  } else {

    // user not authorized. do not publish secrets
    this.stop();
    return;

  }
});

【问题讨论】:

  • 我没有看到任何明显的东西。如果没有看到一些具体的例子,很难说清楚。您能否发布已发布但未发布的文档以及发布该文档的userId?尝试记录userId 或在meteor shell 会话中运行查询。
  • 您好 MasterAm。感谢您的回复。我对上面代码的解释是它发布用户为"accountStatus.isUserAccountActive": true 的用户,然后发布所有类集合。类集合不取决于上面的集合。我想我需要一个 if 语句,如果 "accountStatus.isUserAccountActive": true 然后发布所有这些学生帐户。
  • 您要发布teacherUserId 是当前用户ID 并且studentUserId 属于accountStatus.isUserAccountActive 状态为1 的用户的类吗?如果是这样,那么您正在尝试执行所谓的join,这是 MongoDB 的一个痛点。这是不容易做到的。您可以对数据进行反规范化(然后您必须在数据更改时对其进行同步)或在客户端上手动执行连接(如果您可以找到用户并且用户的帐户处于活动状态,则显示该类),使用这样的包作为 publish-composite 或 MongoDB 的聚合。
  • 是的,这听起来很正确。非规范化数据是什么意思。你的意思是我应该把所有东西都放在一个模式中吗?
  • 不。反规范化是拥有相同数据的多个副本。数据库规范化是相反的动作——消除重复。非规范化在存储空间方面有其自身的成本,增加了复杂性和一致性,仅举几例,但它在许多情况下确实提供了一些性能提升。有多种架构模型可以生成非规范化数据,但这超出了本次讨论的范围。有很多关于meteor+mongo+joins和mongo+de-normalization的阅读材料。

标签: meteor


【解决方案1】:

我相信这应该会让你得到你想要的:

Meteor.publish('classList', function (group) {
  if (Roles.userIsInRole(this.userId, ['is_teacher'], group)) {
    var users = Meteor.users.find({roles:'is_student', "accountStatus.isUserAccountActive": true}, 
      {fields: {
        "profile.firstName": 1, 
        "profile.familyName": 1,
        roles: 1, 
        "accountStatus.isUserAccountActive": 1
      }
    }); // Get active students.

    // Get the user ids of active students.
    var userIdsWhoAreActive = users.fetch().map( function( user ) {
        return user._id;
    });
    return [ users,
    classes.find({teacherUserId: this.userId, studentUserId: { $in: userIdsWhoAreActive },
      {fields: {
        "studentUserId": 1,
        "class": 1
      }})
    ];
  } else {

    // user not authorized. do not publish secrets
    this.stop();
    return;

  }
});

在英语中,我认为第二个查询是:“获取所有教师用户 ID 为当前用户 ID 且学生用户 ID 位于活跃学生用户数组中的课程”

【讨论】:

  • 太棒了。谢谢,斯蒂芬。基于 MasterAM cmets,我不确定这是否有缺点,但它似乎正在工作。谢谢大家!
  • 我认为缺点是,当您添加用户时,它不会被动地更新您的类光标,但我无法想象它会经常出现。
  • 啊,好的。谢谢。它会在我能做的地方做。谢谢阿甘!
  • 没问题 - 如果您有兴趣,这里有一篇很好的博客文章解释了如何完全按照您的要求进行操作:discovermeteor.com/blog/reactive-joins-in-meteor
  • 谢谢。我会看看。这可能有点过头了。在此之后,是否可以在一次发布中发布两次集合。我已经尝试过类似的方法,但它似乎不起作用return [ users, classes.find({studentUserId: { $in: userIdsWhoAreActive }, {fields: { "studentUserId": 1, "class": 1 }}), classes.find({teacherUserId: this.userId}) ];
猜你喜欢
  • 1970-01-01
  • 2016-09-12
  • 2018-03-06
  • 2015-02-02
  • 1970-01-01
  • 2015-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多