title: Filter specific elements of ordered array of objects, based on values of previous objects (aggregation framework)
body: <p>I have these documents:</p>
  
  <pre><code>[
      {
          '_id': 1,
          'role': [
              {  // keep this document
                  'plan': 'free',
                  'date': ISODate('2020-01-01')
              },
              {  
                  'plan': 'free',
                  'date': ISODate('2020-01-02')
              },
              {
                  'plan': 'free',
                  'date': ISODate('2020-01-03')
              },
              {  // keep this document
                  'plan': 'pro',
                  'date': ISODate('2020-01-04')
              },
              {
                  'plan': 'pro',
                  'date': ISODate('2020-01-05')
              },
              {
                  'plan': 'pro',
                  'date': ISODate('2020-01-06')
              },
              {  // keep this document
                  'plan': 'free',
                  'date': ISODate('2020-01-08')
              },
              {
                  'plan': 'free',
                  'date': ISODate('2020-01-09')
              }
          ]
      },
      {
          '_id': 2,
          'role': [
              {  // keep this document
                  'plan': 'pro',
                  'date': ISODate('2020-02-05')
              },
              {
                  'plan': 'pro',
                  'date': ISODate('2020-02-06')
              },
              {  // keep this document
                  'plan': 'free',
                  'date': ISODate('2020-02-07')
              },
              {
                  'plan': 'free',
                  'date': ISODate('2020-02-08')
              },
              {
                  'plan': 'free',
                  'date': ISODate('2020-02-09')
              },
              {  // keep this document
                  'plan': 'pro',
                  'date': ISODate('2020-02-10')
              },
              {
                  'plan': 'pro',
                  'date': ISODate('2020-02-11')
              },
              {
                  'plan': 'pro',
                  'date': ISODate('2020-02-12')
              }
          ]
      }
  ]
  </code></pre>
  
  <p>So I have to filter documents, based on the change of the value of <code>plan</code> field.
  I always want to keep the first occurence, but the next document will only be kept if the value of <code>plan</code> field has changed (e.g. <code>free</code> changed to <code>pro</code>, or <code>pro</code> changed to <code>free</code>).</p>
  
  <p><strong>Obs.</strong>: I have more distinct values for the <code>plan</code> field (e.g. <code>premium</code>, <code>admin</code> etc), but I only got two documents for the example.</p>