我想轉換我的記錄中的某些字段,如果有值爲空。我現在可以用空值返回這些記錄,但我不確定如何更新每條記錄。這是我的代碼(Python的+ pyMongo):MongoDB無法更新值
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
我得到一個:類型錯誤:更新()接受至少3個參數(給出2個)。我還想確保代碼在迭代整個集合時更新特定記錄。謝謝 – user94628