title: MongoDb insert or add in embedded document
body: <p>This is my <strong>final</strong> document structure.  I am on <strong><em>mongo 3.0</em></strong>. </p>
  
  <pre><code>   {
       _id: "1",
       stockA: [ { size: "S", qty: 25 }, { size: "M", qty: 50 } ],
       stockB: [ { size: "S", qty: 27 }, { size: "M", qty: 58 } ]
     }
  </code></pre>
  
  <p>I am randomly adding elements inside <code>stockA</code> or <code>stockB</code> collections. 
  I would like come up with a query which would satisfy following rules:</p>
  
  <ol>
  <li>If <code>item</code> is not exist - create whole document and insert item in <code>stock A or B</code> collection. </li>
  <li>If <code>item</code> is exist but <code>stock A or B</code> collection is missing create <code>stock</code> collection and add <code>element</code> inside collection.  </li>
  <li>if <code>item</code> is exist and <code>stock</code> collection is exist just append <code>element</code> inside collection.  </li>
  </ol>
  
  <p>Question: Is it possible to achieve all that in one query? My main requirement that current insert has to be extremely fast and scalable. </p>
  
  <p>If <strong>yes</strong>, could you please help me to come up with required query.</p>
  
  <p>If <strong>no</strong>, could you please guide me how do I achieve requirements in fastest and cleanest way.</p>
  
  <p>Thanks for any help!</p>