title: Mongoose/Node If document exists, edit data, resubmit
body: <p>I'm trying to create a Mongo Document using Mongoose and Node. However, I want to know that it is unique and if it isn't then add a count.  </p>
  
  <p>A simple example would be, create a user with a username.  If username John is taken, iterate through and create one with John1.  However, if John1 is taken, keep going through until JohnX is free and then save the document.</p>
  
  <pre><code>    var record = new Record();
      record.name = 'John';
      record.username = 'John';
  
      var keepGoing = true;
      var x=1;
      while(keepGoing){
          Record.findOne({username: record.username}, function(err, result){
              console.log('Check I get here');
              if(result!=null){
                  // User Exists, try a new username;
                  record.username = 'John' +  x;
                  x++;
              }
              else{
                 keepGoing= false;
                 record.save .....
                 ....
              }
           });
      }
  </code></pre>
  
  <p>This current code ends up going into an infinite loop.  If I remove the While loop my console.log executes, however, when I put it back into the while loop, it doesn't seem to hit my console.log.  Would really appreciate any insights.</p>