title: Mongo query to filter with an array specific index based on parameters
body: <p>I have a few documents in a mongoDB that have the following structure</p>
  <p><a href="https://i.stack.imgur.com/LY1yP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LY1yP.png" alt="enter image description here" /></a></p>
  <p>In a API web application that I am developing with spring boot I have to code the following query. I can receive a voltageLevelCode to filter register that will contains this voltage level code in the array (That it is easy) but the problem is that i can also receive a voltageLevelCode and a Type, so in this case I have to filter documents that will contains this voltage level code in the array and also WITHIN this voltage level code filter the ones that contains this type (But remember, the type within the voltage level)
  I have been trying to write the query but I dont know how to dynamically set the index to filter the types within this voltage level. Something like:</p>
  <pre><code>{&quot;voltageLevel.&lt;TheIndexByTheDefinenVoltageLevelCode&gt;.types&quot; : &quot;X&quot; }
  </code></pre>
  <p>Example:</p>
  <pre><code>public List&lt;MyClassRepresenting&gt; findByFilter(String type,String voltageLevelCode);
  </code></pre>
  <pre><code>{$and: [{'voltageLevel.voltageLevelCode' : ?1 },{'voltageLevel.&lt;HowTogetIndexForSelectingVoltageLevelCode&gt;.types' : ?2}]}
  </code></pre>
  <p>In this case depending on the tensionLevel received the type parameter must filter according to types within this tensionLevel</p>
  <p>Same happens to me with another query. In SQL the equivalent is the SELECT within another SELECT to select the sub registers but no idea about how to do it in mongo.</p>