title: MongoDB - find entries where field with specific name is present, nested anywhere
body: <p>I have a mongoDB database, where some entries have a field of a name &quot;rx&quot; + value of another field in this entry (let's call it rxid), so the final name of this field could look like &quot;rx1234&quot; or &quot;rx2836&quot;. Inside this field there is a list of dictionaries which MAY contain fields with names &quot;A&quot; and &quot;B&quot;. My task is to find how many entries in this database have at least one non-empty field named A or B, no matter where it is nested. I tried to search for nested queries, however it always requires to specify the &quot;parent&quot; field, which in my case is a combination of rx and a value of other field, so it is not a constant name.</p>
  <p>My database schema look something like:</p>
  <pre><code>{ 
      &quot;_id&quot; : 1, 
      &quot;rxid&quot; 1234, 
      &quot;rx1234&quot; : [
          {
              &quot;A&quot; : &quot;somevalue&quot;, 
              &quot;B&quot; : &quot;someothervalue&quot;,  
          }, 
          {
              &quot;A&quot; : &quot;somevalue2&quot;,
          }
      ]
  },
  { 
      &quot;_id&quot; : 2, 
      &quot;rxid&quot; 2345
  }
  </code></pre>
  <p>In this case I expect to count how many objects are structured like _id = 1 and how many like _id = 2 (there may be other fields inside but they are irrelevant)</p>