2014-05-05 63 views
0

我真的很努力用下面的数据构建一个REST API,在客户端一切正常,但是当我需要使POST请求更新时players我无法弄清楚如何查询mongodb。Mongo db - 查询一个嵌套的对象数组

路线

router.get('/api/teams/:teamid/player/:playerid', player.getById); 

MongoDB的查询

module.exports = { 
    getById: function(req, res) { 
     models.Team.findOne({"players.player_name":"Jokim"}, function(err, player) { 
      if (err) { 
       res.json({error: 'player not found.'}); 
      } else { 
       console.log(player); 
       res.json(player);  
      } 
     }); 
    } 
}; 

JSON数据

[ 
    { 
     "__v":0, 
     "_id":"5362dcf7e99615aa392d7d72", 
     "assists":80, 
     "blocks":14, 
     "feed":null, 
     "fouls":20, 
     "made_one":10, 
     "made_three":5, 
     "made_two":15, 
     "missed_one":4, 
     "missed_three":4, 
     "missed_two":20, 
     "percentage":50, 
     "points":44, 
     "rebounds":100, 
     "steals":33, 
     "team_name":"Bulls", 
     "players":[ 
     { 
      "player_name":"Jokim", 
      "_id":"5365f079ed4914600d9342c7", 
      "team_name":"", 
      "team_id":"", 
      "points":0, 
      "made_one":0, 
      "made_two":0, 
      "made_three":0, 
      "missed_one":0, 
      "missed_two":0, 
      "missed_three":0, 
      "percentage":0, 
      "assists":0, 
      "rebounds":0, 
      "steals":0, 
      "blocks":0, 
      "fouls":0, 
      "feed":"" 
     }, 
     { 
      "player_name":"Taj", 
      "_id":"5365f079ed4914600d9342c6", 
      "team_name":"", 
      "team_id":"", 
      "points":0, 
      "made_one":0, 
      "made_two":0, 
      "made_three":0, 
      "missed_one":0, 
      "missed_two":0, 
      "missed_three":0, 
      "percentage":0, 
      "assists":0, 
      "rebounds":0, 
      "steals":0, 
      "blocks":0, 
      "fouls":0, 
      "feed":"" 
     } 
     ] 
    }, 
    { 
     "team_name":"Wizards", 
     "points":44, 
     "made_one":10, 
     "made_two":15, 
     "made_three":5, 
     "missed_one":4, 
     "missed_two":20, 
     "missed_three":4, 
     "percentage":50, 
     "assists":80, 
     "rebounds":100, 
     "steals":33, 
     "blocks":14, 
     "fouls":20, 
     "feed":null, 
     "_id":"5362dcf7e99615aa392d7d75", 
     "__v":0, 
     "players":[ 
     { 
      "player_name":"John Wall", 
      "points":22, 
      "made_one":3, 
      "made_two":4, 
      "made_three":5, 
      "missed_one":2, 
      "missed_two":3, 
      "missed_three":4, 
      "percentage":5, 
      "assists":2, 
      "rebounds":2, 
      "steals":2, 
      "blocks":5, 
      "fouls":3, 
      "feed":null, 
      "facebook_id":null, 
      "_id":"5362dcf7e99615aa392d7d77" 
     }, 
     { 
      "player_name":"Bradley Beal", 
      "points":22, 
      "made_one":3, 
      "made_two":4, 
      "made_three":5, 
      "missed_one":2, 
      "missed_two":3, 
      "missed_three":4, 
      "percentage":5, 
      "assists":2, 
      "rebounds":2, 
      "steals":2, 
      "blocks":5, 
      "fouls":3, 
      "feed":null, 
      "facebook_id":null, 
      "_id":"5362dcf7e99615aa392d7d76" 
     } 
     ] 
    } 
] 
+0

问题是什么?任何错误? – Vinz243

+0

你能更具体吗?你想要做什么行动?我想你想创建一个新的'玩家'子文件,因为你正在创建一个休息API。 – joao

回答

2

当你试图玩家阵列中的搜索,我认为你将不得不使用elemMatch:

请找到与同一文档:http://docs.mongodb.org/manual/reference/operator/projection/elemMatch/

PS:我没有尝试这样做,因为我没有与蒙戈的NodeJS我的系统上现在。

+0

是啊,这听起来是正确的..我摆弄了很多不同的方式,我遇到了,但它没有做任何好事,我没有正确使用它最有可能的..让我读更多,我1+,因为我认为这是正确的轨道.. –

+0

是的,谢谢你,这是美丽的链接:) –

+0

这里是查询'{_id:“5362dcf7e99615aa392d7d72”},{players:{$ elemMatch:{player_name:“Jokim”}}}' :) –

-2

通常人们都在使用PUT方法更新模型。
检查mongodb文档中的update方法。

路线

router.put('/api/teams/:teamid/player/:playerid', player.updatePlayer); 

控制器

module.exports = { 
    updatePlayer: function(req, res) { 
    models.Team.update({"players.player_name":"Jokim"}, {$set: {assist: 100}}, function(err, player) { 
     if (err) { 
     res.json({error: err}); 
     } else { 
     console.log(player); 
     res.json(player);  
     } 
    }); 
    } 
}; 
+0

哈哈认真吗?我的意思是我只是试图首先访问模型... –

+0

当测试时我不想每次运行POST都想获得响应 –