Understanding MongoDb's Aggregation Pipeline performances

Hello, I’m fairly new to MongoDB Aggregation Pipelines. I’m trying to test the performance of MongoDB pipelines for possible use in my company.
For this test, I have generated 50 million documents (using this amazing tool). The document structure it’s fairly simple:

  • id
  • type
  • cost (float)
  • shop_id
  • tx_day
  • tx_month
  • tx_year

The main index is a composite index for day, month, year + shop_id and type.
One of my requirements for aggregation queries is to sum cost for a specific day (or month), filtering by shop_id and type. The pipeline I designed is quite simple, but it does the job and it’s also quite fast, except for the first “execution”.

To give an idea of the performances, on my 2.4Ghz laptop, using a 50 million documents collection, the first execution takes between 2 and 3 seconds and the following executions between 500 and 600 ms.
Overall, these are excellent response times, but I’m wondering why the first execution is always a bit slower?
Is MongoDB caching the “correct” exec plan? Or, am I getting cached results after the second execution?

I would love to understand more, and also how should I go about understanding what MongoDB is doing under the hood in cases like this.


Probably your PC operating system’s disk caching is speeding up the subsequent execution times.