Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.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 cursor=collection.find_one({quot;name";:“rain";})pymongo查询_Python_Pymongo - Fatal编程技术网

Python cursor=collection.find_one({quot;name";:“rain";})pymongo查询

Python cursor=collection.find_one({quot;name";:“rain";})pymongo查询,python,pymongo,Python,Pymongo,这个查询返回游标对象,现在我想把它的结果存储在字符串中,告诉我怎么做,在python中是否可行。 我试过了 obj=next(cursor,None) values=obj['values'] 但这就产生了错误 TypeError: 'dict' object is not an iterator AttributeError: 'str' object has no attribute 'iteritems' 当我试着去做 for i in cursor: string=i

这个查询返回游标对象,现在我想把它的结果存储在字符串中,告诉我怎么做,在python中是否可行。 我试过了

obj=next(cursor,None)
values=obj['values']
但这就产生了错误

TypeError: 'dict' object is not an iterator
AttributeError: 'str' object has no attribute 'iteritems'
当我试着去做

for i in cursor:
    string=i
    j=j+1
    for key,value in i.iteritems():
        temp=[key,value]
        string2.append(temp)
这返回了错误

TypeError: 'dict' object is not an iterator
AttributeError: 'str' object has no attribute 'iteritems'

首先,我不清楚这是dict对象还是str对象,我的主要目的是将此对象的内容复制到字符串中,以便在程序中使用它

该方法从数据库返回单个文档,而不是a,这就是为什么在第一种情况下会出现
TypeError
。也就是说,您得到了属性错误,因为
i
是字典中的键值,因此字符串和字符串没有任何名为
'iteritems'
的属性。你想要的是。

我的pymongo版本是3.0.3