2014-05-05 48 views
0

這裏是我的MongoDB的文件是什麼樣子的想法(當然JSON格式)貓鼬找到一個嵌套的模型?

[ 
    { 
     _id: 50, 
     team_name: 'bulls', 
     players: [ 
      { 
       _id: 100, 
       player_name: 'Jokim' 
      } 
     ] 
    } 
] 

所以當路線api/teams/50/players/100命中我想爲Jokim。我試圖閱讀貓鼬嵌套文件上的其他帖子,但我似乎無法找到我在這裏尋找的答案。我只是想找到文檔,我已經有了路由設置。

models.Team.find({'players._id':req.params.id }, function(err, player) { 
    if (err) { 
     res.json({error: 'player not found.'}); 
    } else { 
     res.json(player); 
    } 
}); 

這是我的架構

var Team = new Schema({ 
    team_name: { type: String }, 
    players: [ 
     { 
      player_name: { type: String }, 
      points:  { type: Number }, 
      made_one:  { type: Number }, 
      made_two:  { type: Number }, 
      made_three: { type: Number }, 
      missed_one: { type: Number }, 
      missed_two: { type: Number }, 
      missed_three: { type: Number }, 
      percentage: { type: Number }, 
      assists:  { type: Number }, 
      rebounds:  { type: Number }, 
      steals:  { type: Number }, 
      blocks:  { type: Number }, 
      fouls:  { type: Number }, 
      feed:   { type: String }, 
      facebook_id: { type: Number } 
     } 
    ], 
    points:  { type: Number }, 
    made_one:  { type: Number }, 
    made_two:  { type: Number }, 
    made_three: { type: Number }, 
    missed_one: { type: Number }, 
    missed_two: { type: Number }, 
    missed_three: { type: Number }, 
    percentage: { type: Number }, 
    assists:  { type: Number }, 
    rebounds:  { type: Number }, 
    steals:  { type: Number }, 
    blocks:  { type: Number }, 
    fouls:  { type: Number }, 
    feed: { type: String } 
}); 
+0

不確定你的意思,因爲沒有任何理由你所做的查詢不起作用。你期望只檢索**嵌套文件嗎? –

+0

是的,我想要獲得一個特定的模型,這裏的路線的目的是getByID –

+0

路線需要得到團隊,然後得到球員。因此,它是一個集合內的集合...''api/teams /:id/players /:id'然後我會使用'req.params.id'作爲通配符 –

回答

1

當你查詢Team,由貓鼬返回的文檔具有的結構相同你的模式,所以你需要從那個結構中拉出你正在尋找的玩家。

使用$ positional projection operator,以確定您的查詢匹配的第一個球員讓你的結果只包含在players陣列字段玩家:

models.Team.findOne(
    {'players._id': req.params.id }, 
    {'players.$': 1}, 
    function(err, team) { 
     if (err || !team || !team.players || !team.players.length) { 
      res.json({error: 'player not found.'}); 
     } else { 
      res.json(team.players[0]); 
     } 
    } 
); 

注意,因爲你只能findOne使用的find代替希望找到包含該球員的單個球隊。

+0

我發現了一種方式,我用這個'{_id:req.params.teamid},{players:{ $ elemMatch:{_id:req.params.playerid}}}'然後在res.json中我使用了'res.json(player [0] .players);'你認爲他們做同樣的事情嗎? –

+0

@MichaelJosephAubry是的,那也可以。 – JohnnyHK

1

那麼可能是你正在尋找的方法使用的aggregate方法。當然,你會從你的路線提取您team_idplayer_id值:

models.Team.aggregate(
    [ 
     // Makes sense to match the document 
     { "$match": { 
      "_id": team_id, 
      "players._id": player_id 
     }}, 

     // Unwind the players array 
     { "$unwind": "$players" }, 

     // Match the player only 
     { "$match": { "players._id": player_id } }, 

     // Just project the player 
     { "$project": { "_id": 0, "player": "$players" } } 
    ], 
    function(err,docs) { 

     // do work here 
    } 
); 

因此,儘管位置投射將使你項目,該項目配套的父文件,在該投影形式的一部分,單個數組元素聚合管道允許您完全重新塑形,以在響應中包含匹配的數組元素。

有關聚合的更多信息,請參閱手冊中的operator reference

但對於該文件一個基本的例子,你提出:

db.teams.aggregate([ 
    { "$match": { 
     "_id": 50, 
     "players._id": 100 
    }}, 
    { "$unwind": "$players" }, 
    { "$match": { "players._id": 100 } }, 
    { "$project": { "_id": 0, "player": "$players" } } 
]) 

給出結果:

{ "player" : { "_id" : 100, "player_name" : "Jokim" } } 
+0

哇這是新的:)好吧,讓我包裹我的頭繞着這個 –

+0

我只是沒有得到這個,我不能得到它工作對不起 –

+0

@MichaelJosephAubry什麼你不能上班?你的模式是嵌入的,因爲你在你的問題中顯示它不是? –