title: Mongodb find the Consecutive Failure Count
body: <p>I have a dataset like this:</p>
  <pre><code>{
      ip: 1.1.1.1,
      process: 123,
      type: failure,
      date: 2021-04-01
  },
  {
      ip: 1.1.1.2,
      process: 124,
      type: failure,
      date: 2021-03-01
  },
  {
      ip: 1.1.1.1,
      process: 123,
      type: failure,
      date: 2021-02-01
  },
  {
      ip: 1.1.1.1,
      process: 123,
      type: success,
      date: 2021-01-01
  }
  </code></pre>
  <p>How can I get the consecutive failure count for a given ip address and process? For example, given the dataset above, if I was to check how many times ip 1.1.1.1 has failed for process 123 before the last success I should get 2. However if the success record was the last record then I should get 0.</p>
  <p>What I have so far is:</p>
  <pre><code>activityLog.find([
    {
      $match: {
        ip: &quot;1.1.1.1&quot;,
        process: &quot;123&quot;
      },
    },
    {
      $sort: {
        date: -1,
      },
    },
    {
      $limit: 10,
    },
    {
      $project: {
        _id: 0,
        type: 1,
      },
    },
  ]);
  </code></pre>
  <p>This gives me a list of all types - sorted and matched</p>