2013-03-04 80 views
0

我有一個團隊同時有hometeam和awayteam變量,我現在有下面的代碼來顯示每場比賽的hometeam的名字,我將如何改變這個,以便我顯示無論對於每場比賽的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); 
    }); 
    }); 

回答

2

你可以將多個populate稱這樣在一起:

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