Search code examples
pythonmongodbpymongodatabase-cursor

how to determine whether a field exists?


I'm connecting to my mongodb using pymongo:

client = MongoClient()
mongo = MongoClient('localhost', 27017)
mongo_db = mongo['test']
mongo_coll = mongo_db['test']   #Tweets database

I have a cursor and am looping through every record:

cursor = mongo_coll.find() 
for record in cursor:    #for all the tweets in the database
    try:
      msgurl = record["entities"]["urls"]    #look for URLs in the tweets
    except:
      continue

The reason for the try/except is because if ["entities"]["urls"] does not exist, it errors out.

How can I determine whether ["entities"]["urls"] exists?


Solution

  • 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.