问题描述:

If I pass an invalid row, it throws an exception. I would like it to return NULL so I can use it to further process the rest of my scripts.

module_id = module_id

mongo_document = mongo_collection.find_one({ '_id': '%s' % module_id}, { '_id':0, 'svn_path':1 })

myvar = mongo_document

for key, value in myvar.iteritems():

print value

网友答案:

Actually your problem isn't in the find_one function. when you ask for a value that doesnt exist its returns a None value to python, my guess that your exception is actually in the for loop. so i would do something like that:

module_id = module_id

mongo_document = mongo_collection.find_one({ '_id': '%s' % module_id}, { '_id':0, 'svn_path':1 })
myvar = mongo_document
if myvar is not None:
   for key, value in myvar.iteritems():
       print value
eilf:
   print "sorry i could find that id"
相关阅读:
Top