I'm very new to Python. In my script I have written code to update MongoDB database.
def mongoInsert(userData, collection):
mconn = Connection(MONGODB_HOST,MONGODB_PORT)
mdb = mconn[MONGODB_DBNAME]
col = mdb[collection]
#The below codeis used to upsert the data in MongoDB.
for user in userData:
print user
col.update({'id': user[0]}, {'$set':user}, True)
return
where userData is a list,
userData = []
When I run the command python script_name.py using putty; the script runs without any error or exception. But when I see try to see the data in MongoDB, there is no data added in databse(I'm not sure, may be data has been added, but as I'm new here, I'm not able to see it). So I'm thinking now if there is any way to get the rows updated in database after the update command(any return type of update command e.g. boolean or integer), I will use it like
success = col.update({'id': user[0]}, {'$set':user}, True)
print success
but it always prints None value of success. So is there any way to check data is added or not, modifying the above code.
I suppose that with the line
col.update({'id': user[0]}, {'$set': user}, True)
where user[0] is a whole document of user collection, you are trying to update document where id is a whole document. You should write smth like:
col.update({'_id': user[0]['_id']}, {'$set': user}, True)
Moreover i don't understand what are you trying to do with the part {'$set': user}
. Read this and this.
To check that your query was ok you should pass the keyword argument safe=True
to the update method:
success = col.update({'id': user[0]}, {'$set': user}, True, safe=True)
print success
Sorry if i did not understand your problem correctly.