Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python的mongodb集合中的特定值?_Python_Mongodb_Dictionary - Fatal编程技术网

使用python的mongodb集合中的特定值?

使用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

我想通过python访问我的集合(mongodb)中的特定数字

我想访问基于城市“boston”的“仅”距离值(204),而不需要任何“距离”字的按键显示。 预期产出:204

mongodb中的集合(json文件):

节目:

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'}}})]