title: Mongodb - find query based on an element of an array
body: <p>I have the following type of document where I have to find instock elements based on a given value. i.e Return all instocks where <code>ele = 5</code>.</p>
  <pre><code>{&quot;item&quot;:&quot;journal&quot;,
   &quot;instock&quot;:[
      { &quot;warehouse&quot;:&quot;A&quot;, &quot;ele&quot;:[2,4,5] },
      { &quot;warehouse&quot;:&quot;C&quot;, &quot;ele&quot;:[8,5,2] },
      { &quot;warehouse&quot;:&quot;F&quot;, &quot;ele&quot;:[3] },
      { &quot;warehouse&quot;:&quot;K&quot;, &quot;ele&quot;:[2,8,4] }
      ]
  }
  </code></pre>
  <p>I tried to use $elemMatch but it just produces the first element.</p>
  <pre><code>db.inventory.find({&quot;item&quot;: &quot;journal&quot;}, {&quot;_id&quot;: 0, &quot;item&quot;: 0, &quot;instock&quot;:{$elemMatch: {&quot;ele&quot;: {$in: [5]}}} })
  </code></pre>
  <p>But it just gives:</p>
  <pre><code>{ &quot;instock&quot; : [ 
      { &quot;warehouse&quot; : &quot;A&quot;, &quot;ele&quot; : [ 2, 4, 5 ] }
  ]}
  </code></pre>
  <p>And the expectation is</p>
  <pre><code>{ &quot;instock&quot; : [ 
      { &quot;warehouse&quot; : &quot;A&quot;, &quot;ele&quot; : [ 2, 4, 5 ] },
      { &quot;warehouse&quot; : &quot;C&quot;, &quot;ele&quot; : [ 8, 5, 2 ] }
  ]}
  </code></pre>
  <p>How should I get the expected result?</p>