title: Updating a nested mongodb collection using a cursor
body: <p>I am new to mongodb and am using it to store a nested document. E.g. Each document contains an array of students for each class. I am trying to update the information in each of the array nodes. Is there a better way to do it instead of updating each of the array element one at a time?</p>
  
  <p>Here is my record in the collection -</p>
  
  <pre><code>{
      “_id” : “23343” ,
      “class” : “Physics”,
      “students”: [ 
          { “id” : “2412” , “name” : "Alice", “mentor” : 0 },
          { “id” : “2413” , “name” : "Bob, “mentor” : 0 },
      ]
  } 
  </code></pre>
  
  <p>There are multiple records like this in the collection.</p>
  
  <p>I have a list of these courses I need to update for each record. For example I get an array of students for the above record to update like this -</p>
  
  <pre><code>{
      “_id” : “23343” ,
      “class” : “Physics”,
      “students”: [ 
          { “id” : “2412” , “name” : "Alice", “mentor” : "Mark" },
          { “id” : “2413” , “name” : "Bob, “mentor” : "Jackson" },
      ]
  } 
  </code></pre>
  
  <p>What is the best way to update the record?</p>
  
  <p>I am using python. Intuitively,I can do a find() on the collection for the course. I get a cursor for that. I can do a for each in cursor. I believe mongodb updates the whole document on update(). </p>
  
  <pre><code>for record in courseCollection.find():
      recordId = record['_id']
      updatedList = getUpdatedStudentList( record['students'])
      updatedRecord = prepareUpdatedRecord(updatedList)
      courseCollection.update( {'_id' : recordId}, updateList)
  </code></pre>
  
  <p>The pymongo documentation site does not talk about the set option in the update function. Unless I use that I believe mongodb updates the whole document. </p>
  
  <p>Also calling update with a query option by passing in the _id seems unnecessary because I just did the query and have a handle to the record. Can I somehow use the cursor to do the update there by not do the query for the update again?</p>