title: MongoDB - multiple groupings in a hystorical session
body: <p>I work with MongoDB on Redash as a way to easily create and analyze reports, graphs and such. I have created a graph that describes the last 12 months of transactions, grouped by month. But now I also need to group these transactions within each month by gateway. I'm currently using a $project followed by a $group stages, as such:</p>
  <pre><code>        {
              &quot;$project&quot;: {
                  &quot;nominal&quot;: {
                      &quot;$divide&quot;: [
                          &quot;$amount&quot;,
                          100
                      ]
                  },
                  &quot;month&quot;: {
                      &quot;$month&quot;: &quot;$created_at&quot;
                  }
          },
          {
              &quot;$group&quot;: {
                  &quot;_id&quot;: &quot;$month&quot;,
                  &quot;total&quot;: {
                      &quot;$sum&quot;: &quot;$nominal&quot;
                  }
              }
          }
  </code></pre>
  <p>The resulting table is a very simple month-to-value relation.
  What I need is a way to separate the quantities within each month by gateway, similar to an SQL line of the sort:</p>
  <pre><code>SELECT [...], sum(amount OVER (PARTITION BY gateway) FROM [...] GROUP BY month, gateway ORDER BY total, month, gateway
  </code></pre>