title: In a mongodb aggregation pipeline, can I filter an array field with $match?
body: <p>My objective is to build an aggregation pipeline, that supports both cursor pagination, facets, and search.</p>
  
  <p>I currently have this in the pipeline:</p>
  
  <pre class="lang-js prettyprint-override"><code>[
    { $search step },
    { $match step },'
    { $facet step},
    {
       $project: {
         category: 1,
         count: { $size: "$docs" },
         docs: 1
       }
    }
  ]
  </code></pre>
  
  <p>That gives me this results:</p>
  
  <pre class="lang-js prettyprint-override"><code>{
    category: [{ _id: "categoryA", count: 100 }, { _id: "categoryB", count: 200 } ],
    count: 50,
    docs: [ { _id: ObjectId(), name: "bob" } ... ] // 50 docs
  }
  
  </code></pre>
  
  <p>Is it somehow possible to reduce the docs array, with the $match operator - without loosing my count and category facet information? The output needs to be:</p>
  
  <pre class="lang-js prettyprint-override"><code>{
    category: // same as above,
    count: 50, (same as above),
    docs: [] // 10 docs - because of pagination
  }
  </code></pre>
  
  <p>Our current implementation for cursor-based pagination implements the pagination lock by building a <code>$match</code> filter query. It would be awesome to re-use this. Otherwise, I'm afraid my only option is to implement the reducer using <a href="https://docs.mongodb.com/manual/reference/operator/aggregation/" rel="nofollow noreferrer">pipe-line operators</a></p>