如果值为空,我正在尝试转换记录中的某些字段。我现在可以返回那些具有空值的记录,但我不确定如何更新每条记录。这是我的代码(Python + pyMongo):
from geopy import geocoders, distance
from pymongo import Connection
import time
db=Connection().blah
coll = db.tweet_info
cursor = coll.find(
{"$and": [
{"coordinates": {"$type":10}},
{"place": {"$ne": None}}
]},
{"coordinates": 1, "place": 1, "time_normal": 1, "_id":1}, tailable = True, timeout = False)
g = geocoders.Google()
while cursor.alive:
counter=0
try:
doc = cursor.next()
your_id = doc['_id']
if not doc['coordinates']:
counter+=1
print doc
placeName = doc['place']['full_name']
loc = g.geocode(placeName)
coll.update({"_id" : your_id},{"$set": {"coordinates": loc}})
time.sleep(0.15)
else:
pass
except (ValueError, geocoders.google.GQueryError):
pass
except StopIteration:
break