title: MongoDB: Count of matching array elements
body: <p>I have a collection called "Lines" with the following structure (basically, I have a lot of documents that contain several arrays and I need to count their elements with conditions).</p>
  
  <pre><code>    {
          "_id" : "201503110040020021",
          "Line" : "1", // several documents may have this Line value
          "LineStart" : ISODate("2015-03-11T06:49:35.000Z"),
          "SSCEXPEND" : [ 
              {
                  "Secuence" : 10,
                  "Title" : 1,
              }, 
              {
                  "Secuence" : 183,
                  "Title" : 613,
              }, 
              ...
          ],
          "SSCCANCELATIONS" : [ 
              {
                  "Secuence" : 34,
                  "Title" : 113,
              }, 
              {
                  "Secuence" : 96,
                  "Title" : 2,
              }, 
              ... 
          ],
          "SSCVALIDATIONS" : [ 
              {
                  "Secuence" : 12,
                  "Result" : 1
              }, 
              {
                  "Secuence" : 15,
                  "Result" : 1,
              },
              {
                  "Secuence" : 18,
                  "Result" : 20,
              },
              ...
          ]
      },
      ...
  </code></pre>
  
  <p>What I need is to count how many elements in those arrays match certain conditions, for instance, 
  I want to count 
  every element in <code>SSCCANCELATIONS</code>, 
  but I only want to count <code>SSCEXPEND</code> elements with <code>Title = 1, 
  and SSCVALIDATIONS elements with Result &lt; 10</code></p>
  
  <p>I can get the total number of elements of every array, with </p>
  
  <pre><code>db.Lines.aggregate( { $project: { Line : 1, Validations: { $size: "$SSCVALIDATIONS" }, ... } } ) 
  </code></pre>
  
  <p>But I need to stablish conditions, to get something like:</p>
  
  <pre><code>    {
          "_id" : "201503110040020021",
          "Line" : "1",
          "LineStart" : ISODate("2015-03-11T06:49:35.000Z"),
          "SSCEXPEND" : 15,
          "SSCCANCELATIONS" : 10,
          "SSCVALIDATIONS" : 462
      },
  </code></pre>
  
  <p>In the end, I will need to group the result for <code>Line</code> and <code>LineStart</code>, but I think I already have everything else (I get the date substracting hours, minutes,... from the dates I have).</p>
  
  <p>So the only thing I need to know is how to count only the array elements I really want.</p>
  
  <p>I have read about <code>db.collection.group()</code></p>
  
  <p>but I found the <code>db.collection.group(</code>) method does not work with sharded clusters, so I can't use it.</p>
  
  <p>I have also read this old question:
  <a href="https://stackoverflow.com/questions/3168043/mongodb-count-of-matching-nested-array-elements">MongoDB: Count of matching nested array elements</a>
  which is more or less the same, but it was answered almost five years ago and at the time, the answer was that there's no direct way to do it, so I am asking in case there is a way now.</p>