title: Mongoose - Increment Field On Subdoc if exists, else create new
body: <h1>What I am trying to do.</h1>
  
  <p>I have a <code>userSchema</code> that contains a list of <code>operationCountSchema</code>objects. What I am trying to do, is to create a static method that updates the <code>count</code> field on one of these operation count subdocuments if it exists (identified by a <code>month_id</code>) field. If an <code>operationCountSchema</code> document does not exist for the current month, it should create a new document. Is there a way to achieve this behaviour in mongoose? I have tried using upsert to no avail. How would one do this? Thanks.</p>
  
  <h1>CODE</h1>
  
  <pre><code>var operationCountSchema = mongoose.Schema({
      month_id: String,
      count: { type: Number, default: 0 }
  }, {_id : false});
  
  var userSchema = mongoose.Schema({
      username : { type: String, unique: true, required: true },
      email: { type: String, unique: true, required: true },
      password: String,
      operation_counts: [operationCountSchema]
  });
  
  userSchema.statics.incrementOperationCount = function(userID, callback) {
      var currDate = new Date();
      var dateIdentifier = currDate.getFullYear() + "-" + currDate.getMonth();
      //NEED TO INCREMENT OPERATION COUNT IF ONE FOR MONTH EXISTS, 
      //ELSE IF IT DOES NOT EXIST, CREATE A NEW ONE.
  }
  </code></pre>
  
  <p>Also, any suggestions on alternative ways in which this functionality can be achieved are welcome.</p>