Performance with Nested Expressions in agreggations

Hi, the handouts script, there is the following code in scaling.js:

// within a pipeline, it should look something like the following
/*
  {
    $add: [
      1,
      {
        $multiply: [
          9,
          {
            $divide: [
              { $subtract: [<x>, <x_min>] },
              { $subtract: [<x_max>, <x_min>] }
            ]
          }
        ]
      }
    ]
  }
*/

Is better nested expressions in order improve the performance (it is more complex read the code) or the performance is the same splitting in more stages these expressions?. My solution achieve the same result but I have more stages because I need understand the code.

Regards

You will need to test it out to know the impact. Look at the explain plan for each query to see which one performs best.

It’s best to $match first so that you can make use of indexes as much as possible.

1 Like