title: Conditional upsert (insert) when updating document in MongoDB
body: <p>I have some documents in a MongoDB which looks like that:</p>
  
  <pre><code>{type: type1, version: 2, data: ...}
  {type: type1, version: 3, data: ...}
  {type: type2, version: 1, data: ...}
  {type: type2, version: 2, data: ...}
  ...
  </code></pre>
  
  <p>I would like to update the data for matching <em>type</em> AND <em>version</em> or create a new document for given <em>type</em> when <em>version</em> mismatch but would like to prohibit creating new documents with new <em>type</em>
  When I do:</p>
  
  <pre><code>db.getCollection('products').update({"type": "unknown_type", "version" : "99"}, {$set: {"version": 99, "data": new data}}, {"upsert": true})
  </code></pre>
  
  <p>it creates a new document:</p>
  
  <pre><code>{type: unknown_type, version: 99, data: ...}
  </code></pre>
  
  <p>which is exactly what I would like to prohibit.
  Is there a way to do this operation in one call ? Is there a way to restrict values for some fields ?</p>