如何查询此MongoDB文档中的player_id

时间:2014-02-06 17:25:24

标签: mongodb mongoid

这是我在Mongo数据库中的示例文档:

    "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 Mongo shell或Mongoid,如何获取包含例如player_id: 1293的播放器哈希?

问题是,我为NBA的每支球队收集了这样的文件。我希望能够构建一个用户界面,允许您查询单个播放器。很高兴不必查询团队然后过滤数组,并能够搜索每个团队的球员阵列;应该有一种方法来对此进行索引以加快查找时间(存储不是问题)

2 个答案:

答案 0 :(得分: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字段是否有意义?只要该数组的成员没有其他字段,就不需要它。

答案 1 :(得分:0)

我相信你要找的是点符号。查询如下:

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

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

查询将类似于以下内容:

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