0

My document structure is something like this....

enter image description here

I ran this query

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

test_run_names is of type cursor.

How can i iterate over it and get the attributes i need ?

What i did ?

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

1 Answer 1

1

Maybe you have an element without name.

Try this instead:

for t in test_run_names 
    print (t.get('name'))
Sign up to request clarification or add additional context in comments.

7 Comments

Ok, i am trying it out, but isnt t['name'] same as t.get('name') ?
t['name'] will throw an error if name doesn't exist and t.get('name') will return none if there's no name variable
I mean, it will throw an error if name is not defined
Every get('name') shows none , why is that ? Do i need to put projection too ? I mean cursor is returning only the object id i suppose. So that is why maybe every get operation returns None . Am i right ? If yes , how do i get other attributes ?
Maybe you do have none in every document. You can print t just to make sure the data you are getting makes sense.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.