1

I'm writing a small web app with the Pyramid framework (which is awesome) and I'm having trouble updating a document. I have no problems querying for a document with the same id- I just can't seem to update it. Here is the code I'm using:

  for key, value in request.POST.iteritems():
    if value:
      to_insert[key] = value  

  if "_id" in request.POST:
   try:
     _id = ObjectId(request.matchdict['id'])
   except InvalidId:  
     return Response("Error generating id")    

  request.db['concerts'].update( 
    { '_id': _id  }, 
    { "$set": to_insert }, 
    upsert=False
   )  

If I do:

request.db['concerts'].find( {'_id' : _id } 

I find the document that I wish to update, so it doesn't seem to be an issue of the id not being there. It just doesn't seem to commit anything to the database. The only issue I could see is that I update the entire document- I don't check the fields beforehand. Is this the proper way to go about this? I didn't see much in the documentation about updating an indiscriminate number of fields.

tshepang
  • 12,111
  • 21
  • 91
  • 136
  • 1
    Turns out the issue was that I had "_id" in my request.POST (I had it on a form that I was submitting from). The fix to this was: del to_insert['_id'] request.db['concerts'].update( { "_id": _id }, to_insert , Safe = True ) Adding the Safe parameter told me: OperationFailure: cannot change _id of a document old Which makes sense since I was giving it two ids. Boy do I feel like an idiot. – i_thought_i_was_smart Jul 19 '12 at 01:46
  • 3
    You should make you comment an answer to mark the problem as solved. Also, remember to never use direct input from the user. In this example, a user could post massive amount of data to you app, that would end up directly in you database, potentially wasting a lot of space and maybe generating costs. – madjar Nov 08 '12 at 15:00

0 Answers0