Meteor/Mongodb - 数组、子文档和重叠订阅

Meteor/Mongodb - arrays, subdocuments & overlapping subscriptions

我正在制作游戏;玩家组成联盟并做出相互竞争的预测。联赛看起来像这样:

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

对于每个球员,我需要向客户端发布联盟中所有球员的名字,但只有他们自己的秘密预测。所以从上面看,如果 Goodie 登录了,mini-mongo 上的文档应该是:

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

为此,我有两个出版物 - 一个获取整个联盟文档但不包括所有秘密预测,另一个获取玩家数组中当前玩家的子文档包括她的秘密预测。我的出版物是:

// Publish whole players array excluding secretPrediction
Leagues.find({"players.name": "Goodie"}, {fields: {"players.secretPrediction": 0}})

// Publish the whole Goodie item in the players array and nothing else 
Leagues.find({"players.name": "Goodie"}, {fields: {players: {$elemMatch: {name: "Goodie"}}}})

问题是,当我订阅两个 上述出版物时,我没有得到我想要的文件——即使是第二个出版物,秘密预测也被排除在外。 (就其本身而言,这些出版物的行为符合预期,只有当我同时订阅两者时才会如此。)

现在,我从this answer了解到这两个发布应该是"merged"在客户端

Down to the level of top level fields, Meteor takes care to perform a set union among documents, such that subscriptions can overlap - publish functions that ship different top level fields to the client work side by side and on the client, the document in the collection will be the union of the two sets of fields.

所以我有两个主要问题(做得很好/谢谢你做到了这一步!):

  1. 是否因为我没有处理顶级字段而未合并文档?有解决办法吗?
  2. 我是不是完全用错了方法?有没有更好的方法得到我想要的结果?

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

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

这样就可以在单个查询中查询 return 所有玩家并且只查询给定人员的 playerPrediction。

  1. 是的,合并 Meteor 的多个订阅只适用于顶级字段,在 Meteor 文档中提到:Meteor.subscribe

  2. 我不能说你走错了方向,这真的取决于你的情况,你想要帮助什么功能。仅就我自己而言,我会将上述集合分离为两个单独的集合。因为玩家可能加入很多联赛,而联赛可能有很多玩家,所以他们的关系是many-to-many (n-n). For this kind of relation, we should split them to two collections and use an associative table来反映他们的关系

所以在你的情况下,我会:

联赛合集:

[{
  _id: 'league1',
  name: 'League 1',
  // ...
}]

玩家合集:

[{
  _id: 'player1',
  name: 'Player 1',
  // ...
}]

League2Player合集:

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