Search code examples
mongodbtornadodatabase

Tornado - Replace original data retrieved from mongodb


There's some data stored in mongodb, and retrieved into variable student with student = coll.find_one({"name":"Cammy"}):

{ "_id" : ObjectId("511367bebb8027582a953cce"), "name" : "Cammy", "desc" : "does well in Math" }

I want to change some properties of student and executed: student['desc'] = "does well in Physics". In order to replace the original document, I used coll.save(student). But instead of replacing, a new record with the same name and desc but a different _id appeared. What should I do to replace the original document?


Solution

  • use 
    
    db.coll.findAndModify( {
           query: { name: "cami"},
           update: { $set: { desc: 'does well in physics' } }
    } );