title: How to query nested elements in Firebase?
body: <pre><code>    &quot;John&quot; : {
        &quot;David&quot; : {
          &quot;-KIMA0aPsujdAOpkzP0w&quot; : {
            &quot;message&quot; : &quot;hallu&quot;,
            &quot;sender&quot; : &quot;10154053432889835&quot;,
            &quot;time&quot; : 1463898873196
          }
        }
      },
      &quot;Harry&quot; : {
        &quot;Christina&quot; : {
          &quot;-KIMA0aPsujdAOpkzP0v&quot; : {
            &quot;message&quot; : &quot;hallu&quot;,
            &quot;seen&quot; : true,
            &quot;sender&quot; : &quot;self&quot;,
            &quot;time&quot; : 1463898873195
          }
        },
        &quot;Pierce&quot; : {
          &quot;-KILZ_GH7Ji9hQYNK-6p&quot; : {
            &quot;message&quot; : &quot;Eli there.&quot;,
            &quot;seen&quot; : true,
            &quot;sender&quot; : &quot;179914035712208&quot;,
            &quot;time&quot; : 1463888795301
          },
          &quot;-KIM8yPz2UDOZwHEg_nn&quot; : {
            &quot;message&quot; : &quot;hahjajak&quot;,
            &quot;seen&quot; : true,
            &quot;sender&quot; : &quot;self&quot;,
            &quot;time&quot; : 1463898597847
          },
  }
  </code></pre>
  <p>I wanted to query the count of nodes where &quot;seen&quot; key has value &quot;true&quot; of top node which is <strong>John</strong> OR <strong>Harry</strong> There are multiple child node inside it and each child node has multiple child node which has automatic id set.</p>
  <p>I just want to know the count of objects which has &quot;seen&quot; key set to <strong>true</strong> and also count of objects which has &quot;seen&quot; key set to <strong>false</strong></p>
  <p>I can extract all the values in node as follows:</p>
  <pre><code>ref.observeSingleEventOfType(.Value, withBlock: { snap in
      for (key,value) in snap.value as! NSDictionary {
      }
      
  })
  </code></pre>
  <p>I can loop through the dictionary and count the number of objects manually. But that is not too computationally or data efficient as firebase is volume based.</p>
  <p>What I want is to know if there is any query to count the number of objects whose &quot;seen&quot; key in root node is &quot;true&quot; or whose &quot;seen&quot; key in root node is &quot;false&quot;.</p>
  <p><strong>UPDATE:</strong></p>
  <p>Data Structure</p>
  <p><strong>John</strong>  and <strong>Harry</strong>  are user unique ID. and the node <strong>David</strong> which is immediate child of  <strong>John</strong> node is unique ID of person who the user has chatted with earlier.</p>
  <p>And the <strong>KIMA0aPsujdAOpkzP0w</strong> node represent a unique message from user <strong>John</strong> to/from <strong>David</strong>.</p>
  <p>So what i wanted was to count the number of messages from <strong>David</strong> to <strong>John</strong> not seen by <strong>John</strong></p>