Getting most recent documents in time series collection

Tested and solved for us with 6.0.0-rc13 :slight_smile: Just need to $match, $sort and $limit before doing anything else. This way mongo limits the number of buckets to be unpacked and does not unpack all buckets anymore. For instance:

[
  {
      $match: {
          'meta.ti': 1,
          'meta.type': 'P'
      }
  }, {
      $sort: {
          ts: -1
      }
  }, {
      $limit: 50
  }, 
  ...
]

And if you want to retrieve the next 50 you just use the timestamp of the last element:

[
  {
      $match: {
          'meta.ti': 1,
          'meta.type': 'P'
          "ts" : {$lt: ISODate('2022-07-07T09:59:42.743Z')}
      }
  }, {
      $sort: {
          ts: -1
      }
  }, {
      $limit: 50
  }, 
  ...
]

Using an index on the necessary meta fields makes this blazing fast :slight_smile:

1 Like