从mongo集合中提取数据

时间:2012-11-14 15:52:35

标签: python mongodb twitter pymongo

我正在收集实时推文并存储到一个集合中,我现在想要从集合中的记录中提取信息:

  "place" : { "country_code" : "US", "url" : "http://api.twitter.com/1/geo   /id/01fbe706f872cb32.json", "country" : "United States", "place_type" : "city",  "bounding_box" : { "type" : "Polygon", "coordinates" : [  [ [     -77.119759,      38.791645 ],   [   -76.909393,     38.791645 ],    [   -76.909393,     38.995548  ],   [   -77.119759,     38.995548 ] ] ] }, "full_name" : "Washington, DC",  "attributes" : { }, "id" : "01fbe706f872cb32", "name" : "Washington" }

我只想要coordiante信息,所以使用pymongo我尝试做:

 cursor = coll.find({"place.bounding_box.type" : "Polygon"},{"coordinates" : 1}, tailable = True, timeout = False)

但是这不会返回边界框是键的坐标。

我怎样才能返回这些数据?

由于

1 个答案:

答案 0 :(得分:1)

你必须这样做

cursor = coll.find({"place.bounding_box.type" : "Polygon"}, {"place.bounding_box.coordinates" : 1})

这将以以下格式返回数据:

>> cursor.next()
"place" : {"bounding_box" : { "coordinates" : [  [ [     -77.119759,      38.791645 ],   [   -76.909393,     38.791645 ],    [   -76.909393,     38.995548  ],   [   -77.119759,     38.995548 ] ] ]}

所以要获得你想要的数据:

for doc in cursor:
    print doc["place"]["bounding_box"]["coordinates"]