title: mongodb findOneAndUpdate only if a certain condition is met
body: <p>Following is my mongo db entries.</p>
  
  <pre><code>my-mongo-set:PRIMARY&gt; db.stat_collection.find({name : /s/})
  { "_id" : ObjectId("5aabf231a167b3808302b138"), "name" : "shankarmr", "email" : "abc@xyz", "rating" : 9901 }
  { "_id" : ObjectId("5aabf23da167b3808302b139"), "name" : "shankar", "email" : "abc1@xyz1", "rating" : 10011 }
  { "_id" : ObjectId("5aabf2b5a167b3808302b13a"), "name" : "shankar1", "email" : "abc2@xyz2", "rating" : 10 }
  { "_id" : ObjectId("5aabf2c2a167b3808302b13b"), "name" : "shankar2", "email" : "abc3@xyz3", "rating" : 100 }
  </code></pre>
  
  <p>Now i want to find an entry based on name but update a field only if a certain condition holds good.</p>
  
  <p>I tried the following statement, but it gives me error at the second reference to $rating.</p>
  
  <pre><code>db.stat_collection.findOneAndUpdate({name: "shankar"}, {$set : {rating : {$cond : [ {$lt : [ "$rating", 100]}, 100, $rating]}}, $setOnInsert: fullObject}, {upsert : true} )
  </code></pre>
  
  <p>So in my case, it shouldnot update rating for the 2nd document as the rating is not less than 100. But for the third document, rating should be updated to 100.</p>
  
  <p>How do i get it work?</p>