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

cursor= collection.find_one({"name":"rain"}) pymongo query

这个查询 returns 游标对象现在我想将它的结果存储在字符串中如何完成并告诉我是否可以在 python 中。 我试过了

obj=next(cursor,None)
values=obj['values']

但这给出了错误

TypeError: 'dict' object is not an iterator

当我尝试做

for i in cursor:
    string=i
    j=j+1
    for key,value in i.iteritems():
        temp=[key,value]
        string2.append(temp)

这返回了错误

AttributeError: 'str' object has no attribute 'iteritems'

首先,我很困惑这是一个 dict 对象还是 str 对象,我的主要目的是将这个对象的内容复制到一个字符串中,以便我可以在我的程序中使用它

.find_one() method returns a single document from the database not a Cursor that is why you get the TypeError in the first case. That being said you got the attribute error because i is the key value in your dictionary hence string and string doesn't have any attribute named 'iteritems'. What you want is .find().