問題描述
從 mongoDB 中提取信息 (Extracting information from mongoDB)
I have a mongoDB and store coordinates in a collection. If I do:
print doc["coordinates"]
it returns:
{u'type': u'Point', u'coordinates': [‑81.4531344, 28.5287337]}
But if I just want the individual longitudes and latitudes, I'm trying to extract it by:
print doc["coordinates" : [0][1]]
I get nothing printed.
Thanks
‑‑‑‑‑
參考解法
方法 1:
I am not familiar with mongoDB, but if that returned structure is a Python dictionary as it appears, you can access the coordinates like this:
print doc['coordinates']['coordinates']
doc
appears to be a dictionary of dictionaries, and when you access the element coordinates
, you get another dictionary back, and inside this dictionary is another key coordinates
that contains the actual list of coordinates (similarly, if you wanted the type
, you could say print doc['coordinates']['point']
).
Assuming they are in lat, long
format, you could do something like this to pull them into their own variables (this may not suit your case, so feel free to disregard :) ):
lat, long = doc['coordinates']['coordinates']
(by user94628、RocketDonkey)