title: How to remove results that do not meet the filter criteria in MongoDB aggregation pipeline?
body: <p>I am using aggregation over two collections - one of which is nested. The collection looks like this:</p>
  <pre><code>[
    {
      &quot;_id&quot;: &quot;5fe72f0b4fd2c131bcc7dae0&quot;,
      &quot;sirname&quot;: &quot;Smith&quot;,
      &quot;district&quot;: &quot;Texas&quot;,
      &quot;events&quot;: [
        {
          &quot;_id&quot;: &quot;5fe73e91ede45b3d2eca504c&quot;,
          &quot;group&quot;: &quot;Dogs&quot;
        }
      ]
    },
    {
      &quot;_id&quot;: &quot;5fe72f0b4fd2c131bcc7dadf&quot;,
      &quot;sirname&quot;: &quot;Johnson&quot;,
      &quot;district&quot;: &quot;Washington&quot;,
      &quot;events&quot;: [
        {
          &quot;_id&quot;: &quot;5fe73e91ede45b3d2eca504b&quot;,
          &quot;group&quot;: &quot;Cats&quot;
        }
      ]
    },
    {
      &quot;_id&quot;: &quot;5fe72f0b4fd2c131bcc7dade&quot;,
      &quot;sirname&quot;: &quot;MacGruber&quot;,
      &quot;district&quot;: &quot;Washington&quot;,
      &quot;events&quot;: [
        {
          &quot;_id&quot;: &quot;5fe73e91ede45b3d2eca504a&quot;,
          &quot;group&quot;: &quot;Dogs&quot;
        }
      ]
    },
    {
      &quot;_id&quot;: &quot;5fe72f0b4fd2c131bcc7dadd&quot;,
      &quot;sirname&quot;: &quot;Plissken&quot;,
      &quot;district&quot;: &quot;Washington&quot;,
      &quot;events&quot;: [
        {
          &quot;_id&quot;: &quot;5fe73e91ede45b3d2eca5049&quot;,
          &quot;group&quot;: &quot;Cats&quot;
        }
      ]
    },
    {
      &quot;_id&quot;: &quot;5fe72f0b4fd2c131bcc7dadc&quot;,
      &quot;sirname&quot;: &quot;Williams&quot;,
      &quot;district&quot;: &quot;Texas&quot;,
      &quot;events&quot;: [
        {
          &quot;_id&quot;: &quot;5fe73e91ede45b3d2eca5048&quot;,
          &quot;group&quot;: &quot;Dogs&quot;
        }
      ]
    }
  ]
  </code></pre>
  <p>Here is the query code:</p>
  <pre><code>db.collection.aggregate([
    {
      &quot;$match&quot;: {
        district: &quot;Washington&quot;
      }
    },
    {
      $project: {
        sirname: 1,
        district: 1,
        events: {
          &quot;$filter&quot;: {
            input: &quot;$events&quot;,
            as: &quot;event&quot;,
            cond: {
              &quot;$eq&quot;: [
                &quot;$$event.group&quot;,
                &quot;Cats&quot;
              ]
            }
          }
        }
      }
    }
  ])
  </code></pre>
  <p>The result is an array like this:</p>
  <pre><code>[
    {
      &quot;_id&quot;: &quot;5fe72f0b4fd2c131bcc7dadf&quot;,
      &quot;district&quot;: &quot;Washington&quot;,
      &quot;events&quot;: [
        {
          &quot;_id&quot;: &quot;5fe73e91ede45b3d2eca504b&quot;,
          &quot;group&quot;: &quot;Cats&quot;
        }
      ],
      &quot;sirname&quot;: &quot;Johnson&quot;
    },
    {
      &quot;_id&quot;: &quot;5fe72f0b4fd2c131bcc7dade&quot;,
      &quot;district&quot;: &quot;Washington&quot;,
      &quot;events&quot;: [],
      &quot;sirname&quot;: &quot;MacGruber&quot;
    },
    {
      &quot;_id&quot;: &quot;5fe72f0b4fd2c131bcc7dadd&quot;,
      &quot;district&quot;: &quot;Washington&quot;,
      &quot;events&quot;: [
        {
          &quot;_id&quot;: &quot;5fe73e91ede45b3d2eca5049&quot;,
          &quot;group&quot;: &quot;Cats&quot;
        }
      ],
      &quot;sirname&quot;: &quot;Plissken&quot;
    }
  ]
  </code></pre>
  <p>My question is how to exclude from the result the object that does not meet the <code>$filter</code> conditions. That is, I want a result like this:</p>
  <pre><code>[
    {
      &quot;_id&quot;: &quot;5fe72f0b4fd2c131bcc7dadf&quot;,
      &quot;district&quot;: &quot;Washington&quot;,
      &quot;events&quot;: [
        {
          &quot;_id&quot;: &quot;5fe73e91ede45b3d2eca504b&quot;,
          &quot;group&quot;: &quot;Cats&quot;
        }
      ],
      &quot;sirname&quot;: &quot;Johnson&quot;
    },
    {
      &quot;_id&quot;: &quot;5fe72f0b4fd2c131bcc7dadd&quot;,
      &quot;district&quot;: &quot;Washington&quot;,
      &quot;events&quot;: [
        {
          &quot;_id&quot;: &quot;5fe73e91ede45b3d2eca5049&quot;,
          &quot;group&quot;: &quot;Cats&quot;
        }
      ],
      &quot;sirname&quot;: &quot;Plissken&quot;
    }
  ]
  </code></pre>
  <p>Also, i shared the code at <a href="https://mongoplayground.net/p/ZuXdlElICMQ" rel="nofollow noreferrer">mongo playground</a>.</p>
  <p>Regards!</p>