mongodb aggregate pipeline sampling fail

Viewed 41

I'm using mongodb aggregation pipeline with $sampleRate in order to improve my query performances. I felt on a strange behavior i don't understand ...

Here is my aggregation pipeline running on a big collection (1M+ documents) :

[
  {
    '$match': {
      publishedAt: {
        '$gt': new Date('2021-04-27T22:00:00.000Z'),
        '$lt': new Date('2022-04-28T21:59:59.999Z')
      },
      //... some other matching fields
    }
  },
  {
    '$group': {
      _id: {
        keyWords: '$keyWords', // This is an Array<String>
        //... some other fields
      },
      first: { '$first': '$$CURRENT' }
    }
  },
  { '$match': { '$sampleRate': 0.25 } }, // This is where i do my sampling
  { '$replaceRoot': { newRoot: '$first' } },
  {
    '$project': {
      _id: true,
      //... some other fields
    }
  }
]

When i do this i get approximately two times more documents than when i inverse the $replaceRoot and $sampleRate steps =>

[
  {
    '$match': {
      publishedAt: {
        '$gt': new Date('2021-04-27T22:00:00.000Z'),
        '$lt': new Date('2022-04-28T21:59:59.999Z')
      },
      //... some other matching fields
    }
  },
  {
    '$group': {
      _id: {
        keyWords: '$keyWords', // This is an Array<String>
        //... some other fields
      },
      first: { '$first': '$$CURRENT' }
    }
  },
  { '$replaceRoot': { newRoot: '$first' } },
  { '$match': { '$sampleRate': 0.25 } }, // This is where i do my sampling
  {
    '$project': {
      _id: true,
      //... some other fields
    }
  }
]

... I don't understand why oO They should give the same number of documents to me.

Do you know where i'm failing to understand ? Or is it a bug ? :D

1 Answers
Related