如果值为null,我正在尝试转换记录中的某些字段。我现在能够返回那些具有空值的记录,但我不确定如何更新每条记录。这是我的代码(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
答案 0 :(得分:0)
当您有更新时,您需要更新,要更新的文档以及将实际字段更新到的内容:
coll.update({... find ...},
{... update to ...}
)
要获得更新,您需要指定要更新的文档,因此首先必须获取_id
:
"_id": 1,
//在此处附加:{"coordinates": 1, "place": 1, "time_normal": 1}
然后,你必须从游标中获取_id
,最终更新应如下所示:
coll.update({"_id" : your_id}, //
{"$set": {"coordinates": loc}}
)
(在你发表评论后才意识到)