Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Meteor/Mongodb-阵列、子文档和;重叠订阅_Mongodb_Meteor_Publish Subscribe_Subdocument_Minimongo - Fatal编程技术网

Meteor/Mongodb-阵列、子文档和;重叠订阅

Meteor/Mongodb-阵列、子文档和;重叠订阅,mongodb,meteor,publish-subscribe,subdocument,minimongo,Mongodb,Meteor,Publish Subscribe,Subdocument,Minimongo,我在做游戏;球员组成联盟,做出竞争性的预测。联盟是这样的: {联赛名称:“英超联赛”, 玩家:[ {name:“Goodie”,secretPrediction:“abc”}, {姓名:“坏蛋”,秘书长:“def”} ] } 对于每个球员,我需要向客户发布联盟中所有球员的名字,但是只能发布他们自己的秘密预测。因此,从上面看,如果Goodie登录,mini mongo上的文档应该是: {联赛名称:“英超联赛”, 玩家:[ {name:“Goodie”,secretPrediction:“abc”

我在做游戏;球员组成联盟,做出竞争性的预测。联盟是这样的:

{联赛名称:“英超联赛”,
玩家:[
{name:“Goodie”,secretPrediction:“abc”},
{姓名:“坏蛋”,秘书长:“def”}
] }
对于每个球员,我需要向客户发布联盟中所有球员的名字,但是只能发布他们自己的秘密预测。因此,从上面看,如果Goodie登录,mini mongo上的文档应该是:

{联赛名称:“英超联赛”,
玩家:[
{name:“Goodie”,secretPrediction:“abc”},
{名字:“坏蛋”}
] }
为此,我有两份出版物——一份是获取整个联盟文档,但不包括所有秘密预测,另一份是获取玩家数组中当前玩家的子文档,包括她的秘密预测。我的出版物是:

//发布不包括secretPrediction的整个players数组
Leagues.find({“players.name”:“Goodie”},{fields:{“players.secretPrediction”:0})
//在玩家数组中发布整个Goodie项目,而不发布其他内容
Leagues.find({“players.name”:“Goodie”},{fields:{players:{$elemMatch:{name:“Goodie”}}})
问题是,当我订阅上述两种出版物时,我没有得到我想要的文档-即使第二种出版物也不包括秘密预测。(就其本身而言,出版物的行为与预期一致,只有在我同时订阅这两种出版物时才如此。)

现在,我从中了解到,这两个出版物应该在客户端上“合并”

至于顶级字段的级别,Meteor负责在文档之间执行集合合并,以便订阅可以重叠-发布功能将不同的顶级字段并排发送到客户端,在客户端,集合中的文档将是两组字段的合并

因此,我有两个主要问题(做得好/谢谢你这么做!):

  • 文档合并是否因为我没有处理顶级字段而没有发生?有办法解决这个问题吗
  • 我是不是走错了路?有没有更好的方法来获得我想要的结果

  • 您是否可以重新排列数据文档,以便使用单个查询,例如

    { leagueName: "Premier League", 
    players:[ 
             {name: "Goodie"}, 
             {name: "Baddie"} 
            ] 
    playerPredictions:[ 
             {name: "Goodie", secretPrediction: "abc"}, 
             {name: "Baddie", secretPrediction: "def"} 
            ] 
    }
    

    这样,在一个查询中就可以返回所有玩家,并且只返回给定人物的playerPrediction。

    您是否可以重新排列数据文档,以便使用单个查询,例如

    { leagueName: "Premier League", 
    players:[ 
             {name: "Goodie"}, 
             {name: "Baddie"} 
            ] 
    playerPredictions:[ 
             {name: "Goodie", secretPrediction: "abc"}, 
             {name: "Baddie", secretPrediction: "def"} 
            ] 
    }
    
    这样,在一个查询中就可以返回所有玩家,并且只返回给定人员的playerPrediction

  • 是的,合并Meteor的多个订阅仅适用于顶级字段,Meteor文档中提到了这一点:

  • 我不能说你走错了方向,这真的取决于你的情况,你想帮助什么功能。仅就我自己而言,我会将上述集合解耦为两个独立的集合。因为球员可能会加入许多联赛,而联赛可能会有很多球员,所以他们之间的关系非常密切。对于这种关系,我们应该将它们拆分为两个集合,并使用

  • 因此,在你的情况下,我会:

    联盟收藏:

    [{
      _id: 'league1',
      name: 'League 1',
      // ...
    }]
    
    [{
      _id: 'player1',
      name: 'Player 1',
      // ...
    }]
    
    [{
      _id: 'league1palyer1',
      playerId: 'player1',
      leagueId: 'league1',
      secretPrediction: 'abc',
      // ...
    }]
    
    播放器收藏:

    [{
      _id: 'league1',
      name: 'League 1',
      // ...
    }]
    
    [{
      _id: 'player1',
      name: 'Player 1',
      // ...
    }]
    
    [{
      _id: 'league1palyer1',
      playerId: 'player1',
      leagueId: 'league1',
      secretPrediction: 'abc',
      // ...
    }]
    
    League2Player系列:

    [{
      _id: 'league1',
      name: 'League 1',
      // ...
    }]
    
    [{
      _id: 'player1',
      name: 'Player 1',
      // ...
    }]
    
    [{
      _id: 'league1palyer1',
      playerId: 'player1',
      leagueId: 'league1',
      secretPrediction: 'abc',
      // ...
    }]
    
  • 是的,合并Meteor的多个订阅仅适用于顶级字段,Meteor文档中提到了这一点:

  • 我不能说你走错了方向,这真的取决于你的情况,你想帮助什么功能。仅就我自己而言,我会将上述集合解耦为两个独立的集合。因为球员可能会加入许多联赛,而联赛可能会有很多球员,所以他们之间的关系非常密切。对于这种关系,我们应该将它们拆分为两个集合,并使用

  • 因此,在你的情况下,我会:

    联盟收藏:

    [{
      _id: 'league1',
      name: 'League 1',
      // ...
    }]
    
    [{
      _id: 'player1',
      name: 'Player 1',
      // ...
    }]
    
    [{
      _id: 'league1palyer1',
      playerId: 'player1',
      leagueId: 'league1',
      secretPrediction: 'abc',
      // ...
    }]
    
    播放器收藏:

    [{
      _id: 'league1',
      name: 'League 1',
      // ...
    }]
    
    [{
      _id: 'player1',
      name: 'Player 1',
      // ...
    }]
    
    [{
      _id: 'league1palyer1',
      playerId: 'player1',
      leagueId: 'league1',
      secretPrediction: 'abc',
      // ...
    }]
    
    League2Player系列:

    [{
      _id: 'league1',
      name: 'League 1',
      // ...
    }]
    
    [{
      _id: 'player1',
      name: 'Player 1',
      // ...
    }]
    
    [{
      _id: 'league1palyer1',
      playerId: 'player1',
      leagueId: 'league1',
      secretPrediction: 'abc',
      // ...
    }]
    

    1.对的2.您可以尝试仅向客户端发布“虚拟”集合。1。对的2.你可以尝试只向客户发布“虚拟”收藏。接受此作为答案,因为我认为这是获得所需结果的最简单和最快捷的方法。接受此作为答案,因为我认为这是获得所需结果的最简单和最快捷的方法。我在文档中没有看到此评论。“我们希望在未来的版本中取消这一限制。”-希望很快…文档中的好位置-我没有看到这样的评论。“我们希望在未来的版本中取消这一限制。”-希望很快。。。