title: Mongoose update sub document if exists
body: <p>I have following model : </p>
  
  <pre><code>var VoteSchema = new Schema({
      up : Boolean
      , createdBy:{type:ObjectId, ref:'users'}
      , createdOn : {type:Date, default:Date.now}
  });
  
  var QuestionSchema = newSchema({
      title:String
      , description:String
      , votes : [VoteSchema]
      , createdBy:{type:ObjectId, ref:'users'}
      , createdOn : {type:Date, default:Date.now}
  });
  
  var Question = mongoose.model('questions',QuestionSchema);
  </code></pre>
  
  <p>Suppose <code>user1</code> is logged in user and <code>question1</code> is current / viewing question.The user can <code>upvote</code>({up:true}) or <code>downvote</code>({up:false}) a question at any time. <strong>How can I <code>add</code> a new <code>vote</code> if a <code>user1</code> have not casted a vote for <code>question1</code> else <code>update</code> the vote.</strong></p>
  
  <p>I have been able to write the following lines of code:</p>
  
  <pre><code>QuestionSchema.statics.castVote = function(questionId, vote) {
      //NOTE : vote.createdBy equalsto loggedInUserID
  
      Q.nbind(Question.findOne, Question)({
          $and:[
              {_id:questionId},
              {'votes.createdBy':vote.createdBy}
          ]
      }).then(function(doc) {
  
          if(doc) {
             //I am clue less
             //doc.votes is a list of votes for this question
             // how can I get the particular vote casted by the user - vote.createdBy
          }else {
             //Question.votes.push(vote);
          }
      });
  
  });
  </code></pre>