使用python的mongodb集合中的特定值?
我想通过python访问我的集合(mongodb)中的特定数字 我想访问基于城市“boston”的“仅”距离值(204),而不需要任何“距离”字的按键显示。 预期产出:204 mongodb中的集合(json文件): 节目:使用python的mongodb集合中的特定值?,python,mongodb,dictionary,Python,Mongodb,Dictionary,我想通过python访问我的集合(mongodb)中的特定数字 我想访问基于城市“boston”的“仅”距离值(204),而不需要任何“距离”字的按键显示。 预期产出:204 mongodb中的集合(json文件): 节目: d=collection.find_one({"Test_description":{"$elemMatch":{"City":"boston"}}}) print (d['Test_description']['Distance']) 错误: -->TypeE
d=collection.find_one({"Test_description":{"$elemMatch":{"City":"boston"}}})
print (d['Test_description']['Distance'])
错误:
-->TypeError: list indices must be integers or slices, not str
但是当我使用
print(d['Test_description'])
时,它会给我输出{City:voston,distance:204,high_temp:1}
这不是我想要的。假设你使用的是PyMongo
如果只有一个结果
如果有多个结果
-->TypeError: list indices must be integers or slices, not str
distance = collection.find_one({'Test_description':{'$elemMatch':{'City':'boston'}}})['Test_description']['Distance']
distance = [x['Test_description']['Distance'] for x in collection.find({'Test_description':{'$elemMatch':{'City':'boston'}}})]