how to determine whether a field exists?
Record is a dictionary in which the key "entities"
links to another dictionary, so just check to see if "urls"
is in that dictionary.
if "urls" in record["entities"]:
If you just want to proceed in any case, you can also use get.
msgurl = record["entities"].get("urls")
This will cause msgurl to equal None if there is no such key.
I'm not familiar with pymongo, but why don't you change your query so it only returns results that contain "urls"
? Something like:
mongo_coll.find({"entities.urls": {$exists:1}})
http://docs.mongodb.org/manual/reference/operator/exists/