我連接我的mongodb
使用pymongo
到:如何判斷一個字段是否存在?
client = MongoClient()
mongo = MongoClient('localhost', 27017)
mongo_db = mongo['test']
mongo_coll = mongo_db['test'] #Tweets database
我有一個光標和我通過每一個創紀錄的循環:
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
的原因try/except
是因爲如果["entities"]["urls"]
不存在,它錯誤了。
我怎麼能確定[ 「實體」] [ 「網址」]是否存在?
也請更正我的術語「字段」 –