【问题标题】:Meteor-publish returns more rows then required from MongoDBMeteor-publish 返回比 MongoDB 所需的更多行
【发布时间】:2019-02-04 07:17:21
【问题描述】:

我有一个收藏:

{
    "_id" : "SeGtBvCT7ojF2v5x9",
    "teamId" : "d74JJ9s5k6tijeQaz",
    "userScores" : [ 
        {
            "userId" : "6ghphqzx9GFnvKYKY",
            "scores" : 10,
            "addedAt" : ISODate("2019-02-04T06:37:06.387Z")
        }, 
        {
            "userId" : "56ghp45hqzx9G2dda",
            "scores" : 1,
            "addedAt" : ISODate("2019-02-04T06:37:19.105Z")
        }, 
        {
            "userId" : "wrr3422fwefx6fFGs",
            "scores" : 4,
            "addedAt" : ISODate("2019-02-04T06:37:44.005Z")
        }
    ]
}

我需要为一个 teamId 和当前用户 ID (this.userId) 返回“userScores”。所以我做了这个发布方法。

Meteor.publish('scoresTeamByUser', function(teamId) {
  return Scores.find(
      { teamId }, 
      { userScores: { $elemMatch: { userId: this.userId } } }
  );
});

但在流星/反应应用程序中,我收到 (this.props.receivedScores) 整个文档,所有行都在“userScores”中。

export default withTracker(props => {
    const scoresSubscription = Meteor.subscribe('scoresTeamByUser', props.teamId);
    return {
        receivedScores: Scores.findOne(),
        scoresLoaded: scoresSubscription.ready()
    };
})(GiveScores);

如何只获取一个团队和一个给出分数的用户的数据?谢谢:)

【问题讨论】:

    标签: mongodb meteor publish-subscribe meteor-publications


    【解决方案1】:

    我检查了您的查询,它工作正常,并且只返回与 userId 匹配的 userScores 数组中的一个对象。

    您需要使用fields 来过滤您要发布的字段。

    Meteor.publish('scoresTeamByUser', function(teamId) {
      return Scores.find(
          { teamId }, 
          { fields: { userScores: { $elemMatch: { userId: this.userId }}} }
      );
    });
    

    你得到userScores 数组中的所有对象的原因是你必须有另一个订阅发布整个记录。在订阅scoresTeamByUser 出版物之前,您可以通过console.log(Scores.findOne({ props.teamId})) 进行检查。

    因此,您必须找到该发布并将其限制为仅发布当前用户的分数,或者在您当前的订阅中,您必须过滤客户端查询中的数据,如下所示。

    export default withTracker(props => {
        const scoresSubscription = Meteor.subscribe('scoresTeamByUser', props.teamId);
        return {
            receivedScores: Scores.findOne({ teamId: props.teamId }, 
      { userScores: { $elemMatch: { userId: Meteor.userId() } }),
            scoresLoaded: scoresSubscription.ready()
        };
    })(GiveScores);
    

    【讨论】:

    • 我会试试看!会带着结果回来。谢谢! :)
    • 你好。我不会在任何其他地方发布分数集合。所以我不明白为什么它会这样工作。 ???有什么想法吗?
    • 我在这里找到了这样的例子docs.mongodb.com/manual/reference/operator/projection/elemMatch,但为什么我在 Robo 3T 工作,而不是在 Meteor?
    • 对不起,伙计,我的错。更新答案
    • 是的!这正是我正在寻找的。 Meteor 在 $elemMatch 之前添加了“字段”。现在它起作用了。谢谢! :)
    【解决方案2】:

    听起来你想要这样的东西。

    Meteor.publish('scoresTeamByUser', function(teamId) {
    
        const teamScores = Scores.find({ teamId: teamId });
    
        const userScore = Scores.find(
        {'userScores.userId': this.userId}, {'userScores.$': 1});
    
        return [teamScores, userScore];
    });
    

    【讨论】:

    • 其实我看不出它有什么帮助。
    猜你喜欢
    • 1970-01-01
    • 2013-03-08
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2016-03-06
    • 1970-01-01
    相关资源
    最近更新 更多