I'm using a for loop in python to loop over the result of a query with pymongo. Here is the code:
from pymongo import MongoClient
connection = MongoClient()
db = connection.Test
myDocs = db.Docs.find( { "geolocCountry" : { "$exists" : False } } )
for b in myDrives:
my_lat = b['TheGpsLog'][0]['latitude']
my_long = b['TheGpsLog'][0]['longitude']
myGeolocCountry = DoReverseGeocode(lat_start,long_start)
# Here I perform a reverse geocoding, it does not matter for this example.
# The important thing is: it returns a string, like 'US', 'UK', etc...
The question I have is, how can I insert the variable myGeolocCountry
into the non existing field geolocCountry
on the existing document (b
)?
I tried with
b['geolocCountry'] = myGeolocCountry
but it didn't work at all, it does not even produce an error.
Thanks
You should execute an update query like this:
db.Doc.update({"_id": b["_id"]}, {"$set": {"geolocCountry": myGeolocCountry}})