添加一个重度嵌套的词典到MongoDB保留其格式

时间:2017-03-31 10:01:08

标签: python mongodb python-2.7 pymongo

我使用python从Facebook GraphAPI中提取了一些数据并将其保存到变量中。 API返回JSON,它在Python中保存为嵌套字​​典。这就是API中的数据在浏览器中的样子。

enter image description here

Python dict看起来像这样: enter image description here

我希望mongoDB以FB API返回的健全的JSON格式存储数据。这可能吗?

代码:

import facebook

graph = facebook.GraphAPI(access_token='my_token', version='2.7')

post = graph.get_object(id='me/friends', fields='feed{message,story,story_tags,created_time,id,message_tags},posts{message,story,story_tags,message_tags,created_time,id},name')
print(post)

请帮助。

1 个答案:

答案 0 :(得分:-1)

是:

from pymongo import MongoClient
db = MongoClient().test
db.collection.insert_one(post)

The PyMongo tutorial是学习Python和MongoDB的好方法。