使用PyMongo

时间:2016-12-12 23:15:46

标签: json mongodb python-2.7 pymongo

假设我在MongoDB数据库中有以下文档:

{
    "assist_leaders" : {
        "Steve Nash" : {
            "team" : "Phoenix Suns",
            "position" : "PG",
            "draft_data" : {
                "class" : 1996,
                "pick" : 15,
                "selected_by" : "Phoenix Suns",
                "college" : "Santa Clara"
            }
        },
        "LeBron James" : {
            "team" : "Cleveland Cavaliers",
            "position" : "SF",
            "draft_data" : {
                "class" : 2003,
                "pick" : 1,
                "selected_by" : "Cleveland Cavaliers",
                "college" : "None"
            }
        },
    }
}

我正在尝试在"draft_data"下为ORDERED列表中的每个玩家收集一些值。对于此特定文档,列表需要如下所示:

[ [1996, 15, "Phoenix Suns"], [2003, 1, "Cleveland Cavaliers"] ]

也就是说,每个嵌套列表必须按顺序包含与“pick”,“selected_by”和“class”键对应的值。我还需要在“勒布朗詹姆斯”数据之前提供“史蒂夫纳什”数据。

如何使用pymongo实现这一目标?请注意,数据的结构不是一成不变的,所以我可以更改它,如果这样可以使代码更简单。

2 个答案:

答案 0 :(得分:0)

在您从MongoDB检索文档后,我将提取数据并将其转换为Python列表:

for doc in db.collection.find():
    for name, info in doc['assist_leaders'].items():
        draft_data = info['draft_data']
        lst = [draft_data['class'], draft_data['pick'], draft_data['selected_by']]
        print name, lst

答案 1 :(得分:0)

列表理解是这里的方法(注意:不要忘记Python2中的.iteritems()或Python3中的.items()或者你会得到ValueError: too many values to unpack)。

import pymongo
import numpy as np
client = pymongo.MongoClient()
db = client[database_name]

dataList = [v for i in ["Steve Nash", "LeBron James"]
            for key in ["class", "pick", "selected_by"]
            for document in db.collection_name.find({"assist_leaders": {"$exists": 1}})
            for k, v in document["assist_leaders"][i]["draft_data"].iteritems()
            if k == key]

print dataList
# [1996, 15, "Phoenix Suns", 2003, 1, "Cleveland Cavaliers"]

matrix = np.reshape(dataList, [2,3])

print matrix
# [ [1996, 15, "Phoenix Suns"],
#   [2003, 1, "Cleveland Cavaliers"] ]