猫鼬.Populate有2个条件

时间:2013-03-04 16:12:33

标签: javascript mongoose

我有一个同时拥有hometeam和awayteam变量的团队,我现在有下面的代码来显示每场比赛的家庭团队的名称,我将如何改变这一点,以便我显示家庭团队和远程团队每个匹配,使它看起来像下面这样:

[
  "Team 1", "Team 2",
  "Team 3", "Team 4"
]

其中1/2队在一场比赛中,而3/4队在另一场比赛中。

  app.get('/homeTeamNames', function(req, res) {
    util.log('Serving request for url [GET] ' + req.route.path);
    Match.find({}, {'hometeam': 1}).populate('hometeam', {name: 1}).exec(function(err, teams) {
      var homeNames = [];
      for(var i = 0; i < teams.length; i++) {
        homeNames.push(teams[i].hometeam.name);
      }
      res.send(homeNames);
    });
  });

1 个答案:

答案 0 :(得分:2)

您可以像这样链接多个populate个来电话:

Match.find({}, {hometeam: 1, awayteam: 1})
    .populate('hometeam', {name: 1})
    .populate('awayteam', {name: 1})
    .exec(function(err, teams) { ...