title: Mongo Age Group Aggregation
body: <p>Please consider the collection bellow</p>
  
  <pre><code> $people-&gt;insert(array("user_id" =&gt; "1", "day" =&gt; "Monday", 'age' =&gt; 18));
   $people-&gt;insert(array("user_id" =&gt; "3", "day" =&gt; "Monday", 'age' =&gt; 24));
   $people-&gt;insert(array("user_id" =&gt; "1", "day" =&gt; "Monday", 'age' =&gt; 18));
   $people-&gt;insert(array("user_id" =&gt; "1", "day" =&gt; "Monday", 'age' =&gt; 18));
   $people-&gt;insert(array("user_id" =&gt; "2", "day" =&gt; "Monday", 'age' =&gt; 25));
   $people-&gt;insert(array("user_id" =&gt; "4", "day" =&gt; "Monday", 'age' =&gt; 33));
   $people-&gt;insert(array("user_id" =&gt; "1", "day" =&gt; "Tuesday", 'age' =&gt; 18));
   $people-&gt;insert(array("user_id" =&gt; "2", "day" =&gt; "Tuesday", 'age' =&gt; 25));
   $people-&gt;insert(array("user_id" =&gt; "1", "day" =&gt; "Wednesday", 'age' =&gt; 18));
   $people-&gt;insert(array("user_id" =&gt; "2", "day" =&gt; "Thursday", 'age' =&gt; 25));
   $people-&gt;insert(array("user_id" =&gt; "1", "day" =&gt; "Friday", 'age' =&gt; 18));
  </code></pre>
  
  <p>Can anyone help me to get count of distinct users within a age group?
  For example for the schema above i would like to get</p>
  
  <pre><code>      Age 0-17 = 0, Age 18-25 = 3, Age 26-32 = 0 Age &gt; 32 = 1
  </code></pre>
  
  <p>I have tried to use the <a href="http://docs.mongodb.org/manual/reference/operator/aggregation/cond/" rel="nofollow">$cond</a> operator but didn't manage to get it it work.
  Every time i try to run or change it i get one of the 2 errors:</p>
  
  <ol>
  <li>The "$cond" operator requires 3 operands 
  or</li>
  <li>A pipeline stage specification object must contain exactly one field.</li>
  </ol>
  
  <p>My query is bellow any help is much appreciated. Thanks in advance,</p>
  
  <pre><code>    $query =
          array(
             $project' =&gt; array(
                  ageGroup' =&gt; array(
                     array('$cond'=&gt;  array('$user_data.age' =&gt; array('$lt' =&gt; 18),
                                             "age_0_17",
                     array('$cond'=&gt;  array('$user_data.age' =&gt; array('$lte' =&gt; 25),
                                             "age_18_25",
                     array('$cond'=&gt;  array('$user_data.age' =&gt; array('$lte' =&gt; 32),
                                             "age_26_32",
                                             "age_Above_32")))))
                      )
                  ),
              ),
  
              array(
                  '$group' =&gt; array(
                      '_id'  =&gt; '$ageGroup',
                      'count' =&gt; array('$sum' =&gt; 1),
                  )
              ));
  </code></pre>
  
  <p>The Answer by @Neil Lunn is  90% right, it did not give me the desired output  but leaded 
  took me there.</p>
  
  <p>With Neil's query the output i get is: </p>
  
  <pre><code>age_Above_32 = 1 and age_18_25 = 10 
  </code></pre>
  
  <p>The output for distinct user_id count should be</p>
  
  <pre><code>age_Above_32 = 1 and age_18_25 = 3 
  </code></pre>
  
  <p>To obtain that i just had to tweak Neil's query a little bit.
  The final query is below.</p>
  
  <pre><code>$query2 = array(
          array(
              '$group' =&gt; array(
                  '_id' =&gt; array(
                      'ageGroup' =&gt; array(
                          '$cond' =&gt;  array(
                              array('$lt' =&gt; array( '$age', 18 )),
                              'age_0_17',
                              array(
                                  '$cond' =&gt; array(
                                      array( '$lte' =&gt; array( '$age', 25 )),
                                      'age_18_25',
                                      array(
                                          '$cond' =&gt; array(
                                              array( '$lte' =&gt; array ( '$age', 32 )),
                                              'age_26_32',
                                              'age_Above_32'
                                          )
                                      )
                                  )
                              )
                          )
                      ),
                      'user_id' =&gt;'$user_id'
                  )
              )
  
          ),
          array(
              '$group' =&gt; array(
                  '_id'  =&gt; '$_id.ageGroup',
                  'count' =&gt; array('$sum' =&gt; 1)
              ))
      );
  </code></pre>