title: mongodb update not working to replace a document (but $set does)
body: <p>From homework 2_2 of the M101JS course at university.mongodb.com.</p>
  
  <blockquote>
    <p>Write a program that finds the document with the highest recorded temperature for each state, and adds a "month_high" field for that document, setting its value to true.</p>
  </blockquote>
  
  <p>The following code should work to update the correct doc in the collection adding "month_high". And the problem is here:</p>
  
  <pre><code>//db.collection('data').update(query, setMonthHigh, function(err, updated) {//OK
  db.collection('data').update(query, doc, function(err, updated) {// NOT OK, dunno why
  </code></pre>
  
  <p>The 2nd update line does not save anything. But if I use the commented update above, it works.</p>
  
  <p>Does anyone have an idea why the 2nd update line isn't working (the one that replaces the whole doc)?</p>
  
  <pre><code>var MongoClient = require('mongodb').MongoClient;
  
  MongoClient.connect('mongodb://localhost:27017/weather', function(err, db) {
  
  if(err) throw err;
  
  var options = { 'sort' : [ ['State', 1], ['Temperature', -1] ]};
  var lastDoc = null;
  var setMonthHigh = {'$set':{'month_high':true}};
  
  var cur = db.collection('data').find({}, {},options);
  //var cur = db.collection('data').find({'month_high':true}, {},options);
  
  cur.each(function(err, doc) {
      if(err) throw err;
  
      if(doc == null) {
          console.log("Document done");
          //return db.close();
          db.close();
          process.exit(0);
      }
  
      var query = {'_id' : doc._id};
  
      //db.collection('data').update({'month_high':true}, {$unset:{'month_high':1}}, function(err, updated) {});
      //console.dir(doc);
  
      if ( ( lastDoc != null &amp;&amp; lastDoc.State != doc.State ) ||
          lastDoc == null )
      {
          doc['month_high'] = 'true';
          //db.collection('data').update(query, setMonthHigh, function(err, updated) {//OK
          db.collection('data').update(query, doc, function(err, updated) {
              // NOT OK, dunno why
              if(err) throw err;
              console.dir("Update "+updated);
          });
          console.dir(doc);
      }
      lastDoc = doc;
  });
  });
  </code></pre>
  
  <p>Also, I'm told that using <code>process.exit(0)</code> is a bad hack, but it was the only way I could exit the code without throwing exceptions on return <code>db.close();</code>. If anyone knows a way to exit the code gracefully, that would be great too.</p>