title: mongodb group values by three fields
body: <p>For example, I have these documents:</p>
  <pre><code>[
    {
      &quot;state&quot;: &quot;Jharkhand&quot;,
      &quot;district&quot;: &quot;Ranchi&quot;,
      &quot;salesman&quot;: &quot;amit&quot;
    },
    {
      &quot;state&quot;: &quot;Jharkhand&quot;,
      &quot;district&quot;: &quot;Ranchi&quot;,
      &quot;salesman&quot;: &quot;sumit&quot;
    },
    {
      &quot;state&quot;: &quot;Jharkhand&quot;,
      &quot;district&quot;: &quot;Bokaro&quot;,
      &quot;salesman&quot;: &quot;rahul&quot;
    },
    {
      &quot;state&quot;: &quot;Jharkhand&quot;,
      &quot;district&quot;: &quot;Bokaro&quot;,
      &quot;salesman&quot;: &quot;rajesh&quot;
    },
    {
      &quot;state&quot;: &quot;Jharkhand&quot;,
      &quot;district&quot;: &quot;Bokaro&quot;,
      &quot;salesman&quot;: &quot;ravi&quot;
    },
    {
      &quot;state&quot;: &quot;Orisha&quot;,
      &quot;district&quot;: &quot;Puri&quot;,
      &quot;salesman&quot;: &quot;anish&quot;
    },
    {
      &quot;state&quot;: &quot;Orisha&quot;,
      &quot;district&quot;: &quot;Puri&quot;,
      &quot;salesman&quot;: &quot;arjun&quot;
    },
    {
      &quot;state&quot;: &quot;Orisha&quot;,
      &quot;district&quot;: &quot;Puri&quot;,
      &quot;salesman&quot;: &quot;aryan&quot;
    },
    {
      &quot;state&quot;: &quot;Orisha&quot;,
      &quot;district&quot;: &quot;Bhubneswar&quot;,
      &quot;salesman&quot;: &quot;anuj&quot;
    },
    {
      &quot;state&quot;: &quot;Orisha&quot;,
      &quot;district&quot;: &quot;Bhubneswar&quot;,
      &quot;salesman&quot;: &quot;chandan&quot;
    },
    {
      &quot;state&quot;: &quot;Orisha&quot;,
      &quot;district&quot;: &quot;Bhubneswar&quot;,
      &quot;salesman&quot;: &quot;charan&quot;
    }
  ]
  </code></pre>
  <p>and so on.</p>
  <p>How can I make a request, which will describe the N state, N district and N salesman?</p>
  <p>Example of expected result:</p>
  <pre>
  
      {
      "_id" : "state",
      "districts" : [
          {
              "district" : "Ranchi",
              "salesmen" : [
                  {
                      "salesmen" : "amit",
                      "count" : 1
                  },
              {
                  "salesmen" : "sumit",
                      "count" : 1
                  }
              ],
              "count" : 2
          }
      ],
      "total" : 2
  }
  
  </pre>