2014-02-06 44 views
1

这里有一个例子文件我在Mongo的数据库中查询一个player_id:我怎么这个MongoDB的文档

"sport" : "NBA", 
    "team_id" : 111, 
    "season_year" : 2013, 
    "season_type" : "Regular", 
    "title" : "2013-2014 Oklahoma City Thunder Individual Stats", 
    "team_name" : "Oklahoma City Thunder", 
    "players" : [ 
     { 
      "player" : { 
       "player_id" : 1293, 
       "player_name" : "K.Durant", 
       "games_played" : 50, 
       "minutes_played" : 1900, 
       "points_per_game" : 31, 
       "rebounds_per_game" : 7.7, 
       "assists_per_game" : 5.3, 
       "offensive_rebounds" : 39, 
       "defensive_rebounds" : 344, 
       "total_rebounds" : 383, 
       "assists" : 267, 
       "steals" : 74, 
       "blocks" : 41, 
       "turnovers" : 172, 
       "personal_fouls" : 110, 
       "field_goals_made" : 507, 
       "field_goals_attempted" : 989, 
       "field_goal_percentage" : 0.513, 
       "three_points_made" : 110, 
       "three_points_attempted" : 264, 
       "three_point_percentage" : 0.417, 
       "free_throws_made" : 426, 
       "free_throws_attempted" : 483, 
       "free_throw_percentage" : 0.882, 
       "points" : 1550, 
       "highest_points" : 54 
      } 
     }, 
     { 
      "player" : { 
       "player_id" : 1515, 
       "player_name" : "R.Westbrook", 
       "games_played" : 25, 
       "minutes_played" : 821, 
       "points_per_game" : 21.3, 
       "rebounds_per_game" : 6, 
       "assists_per_game" : 7, 
       "offensive_rebounds" : 27, 
       "defensive_rebounds" : 122, 
       "total_rebounds" : 149, 
       "assists" : 174, 
       "steals" : 44, 
       "blocks" : 3, 
       "turnovers" : 100, 
       "personal_fouls" : 53, 
       "field_goals_made" : 190, 
       "field_goals_attempted" : 448, 
       "field_goal_percentage" : 0.424, 
       "three_points_made" : 34, 
       "three_points_attempted" : 110, 
       "three_point_percentage" : 0.309, 
       "free_throws_made" : 119, 
       "free_throws_attempted" : 150, 
       "free_throw_percentage" : 0.793, 
       "points" : 533, 
       "highest_points" : 34 
      } 
     } 
    ] 

使用JavaScript蒙戈外壳或Mongoid,如何做一个查询来获取玩家的哈希包含例如,player_id: 1293

事情是我有一个这样的文件收集每个团队在NBA。我希望能够构建一个用户界面,以便您查询单个玩家。如果不需要查询球队,然后过滤阵列并能够搜索每个球队的球员阵列,那就太好了。应该有一种方法来索引这个以加快查找时间(存储不成问题)

回答

2

使用$elemMatch projection

db.teams.find(
    // find the document that contains said player: 
    {"players.player.player_id" : 1293}, 
    // project so you only get the requested player: 
    {"players" : {$elemMatch : { "player.player_id" : 1293 } }}).pretty(); 

你可以把一个索引上players.player.player_id

结果:

{ 
     "_id" : ObjectId("52f3c81dae88ee90c711184b"), 
     "players" : [ 
       { 
         "player" : { 
           "player_id" : 1293, 
           "player_name" : "K.Durant", 
           // etc. 
         } 
       } 
     ] 
} 

注意,这保留了文档,映射到强类型语言时,这是相当有帮助的原始结构,但通常不是SQL经验的人的期望。

哦,也许这是有道理的,删除玩家阵列多余的player领域?只要该阵列的成员没有附加字段,就不是必需的。

0

我相信你正在寻找的是点符号。查询将如下:

db.sport.find({ "players.player.player_id" : "1293" }) 

但是,这将返回整个文档的团队。如果您只想为播放器提供子文档,则需要使用聚合框架并展开播放器阵列。

查询然后将类似如下:

db.sport.aggregate([ 
{ "$unwind" : "$players" }, 
{ "$match" : { "players.player.player_id" : 1293 } } 
])