2016-06-01 76 views
0

我的文档结构是这样的....如何使用python在mongodb中迭代游标?

enter image description here

我跑这个查询

test_run_names = self._database.suiteruns.find({'user': user_id, {'$or': [{'name': {'$exists': True}},{'name': {'$eq': ""}}]}}) 

test_run_names是游标类型。

如何迭代它并获得我需要的属性?

我做了什么?

for t in test_run_names 
    print (t['name']) #throws a key error 
+0

我建议看看https://api.mongodb.com/python/current/tutorial.html – Saleem

回答

1

也许你有没有名的元素。

试试这个:

for t in test_run_names 
    print (t.get('name')) 
+0

好吧,我想出来的,但isnt t ['name']与t.get('name')相同? – cafebabe1991

+0

如果名字不存在并且t.get('name')将会返回一个错误,如果没有名字变量,那么t.get('name')将返回none –

+0

我的意思是,如果名字没有定义,它会抛出一个错误 –