title: Python MongoDB Nested Group Query
body: <p>I have a really basic questions for you. My documents like this;</p>
  <pre><code>{'_id': ObjectId('60c6f765483c161ebeffa5c3'),
  'User': 'bayram3932',
  'Tweet': '@hfa01 #egeen 1350 üstüne atmalı ,
  'Date': datetime.datetime(2021, 6, 14, 9, 29, 57, 474000),
  'Analysis': {'Polarity': 0.0, 'Subjectivity': 0.0, 'Result': 'Neutral'},
  'Ticker': 'EGEEN',
  'Translated': '1350 HTTPS T CO B NT KR Q FWG W'},
  {'_id': ObjectId('60c6f76e483c161ebeffa5c4'),
  'User': 'Borsatakip56',
  'Tweet': 'Herkese günaydın. Bol bereketli, bol kazançlı bir hafta olsun inşallah.. #XU100  #bist100',
  'Date': datetime.datetime(2021, 6, 14, 9, 30, 6, 401000),
  'Analysis': {'Polarity': 0.7,
  'Subjectivity': 0.6000000000000001,
  'Result': 'Positive'},
  'Ticker': 'XU100',
  'Translated': 'Good morning everybody Plenty of luxuries plenty of professors get a week instruction XU 100 Bi st 100'},
  {'_id': ObjectId('60c6f770483c161ebeffa5c5'),
  'User': 'hisselog',
  'Tweet': 'Teknosa İç ve Dış Ticaret A.Ş. #TKNSA, $TKNSA #BorsaIstanbul #BIST100 #BIST30 #BIST',
  'Date': datetime.datetime(2021, 6, 14, 9, 30, 8, 224000),
  'Analysis': {'Polarity': -0.125,
  'Subjectivity': 0.125,
  'Result': 'Negative'},
  'Ticker': 'TKNSA',
  'Translated': 'Tek nos a Interior and Foreign Trade Inc TK NSA BI ST 100 BI ST 30'},
  </code></pre>
  <p>I wanna query like this dataframe;
  <a href="https://i.stack.imgur.com/txRrN.png" rel="nofollow noreferrer">I wanna query like this dataframe</a></p>
  <p>In other words, I want to query and group positivity, negativity, and neutrality for all tickers.</p>
  <pre><code>hours = 3
  sentiment_pipeline = [{&quot;$match&quot;: 
                              {&quot;Date&quot;: {&quot;$gte&quot;:datetime.datetime.now()-datetime.timedelta(hours = hours),&quot;$lte&quot;:datetime.datetime.now()},
                                     &quot;Analysis.Result&quot;:&quot;Negative&quot;
                              }
                      },
                    {&quot;$group&quot;:{&quot;_id&quot;:&quot;$Ticker&quot;, &quot;count&quot; : {&quot;$sum&quot; : 1}}}
                   ]
  </code></pre>
  <p>The pipeline above shows my attempt. I can easily find and count the negative ones in the last three hours, but I want to do this for all polarity types at once.</p>
  <p>If I do 3 separate query like this;</p>
  <pre><code>sentiment_pipeline = [{&quot;$match&quot;: 
                          {&quot;Date&quot;: {&quot;$gte&quot;:datetime.datetime.now()-datetime.timedelta(hours = hours),&quot;$lte&quot;:datetime.datetime.now()},
                                 ***&quot;Analysis.Result&quot;:&quot;Positive&quot;***
                          }
                  },
                {&quot;$group&quot;:{&quot;_id&quot;:&quot;$Ticker&quot;, &quot;count&quot; : {&quot;$sum&quot; : 1}}}
               ]
  </code></pre>
  <p>and</p>
  <pre><code>sentiment_pipeline = [{&quot;$match&quot;: 
                      {&quot;Date&quot;: {&quot;$gte&quot;:datetime.datetime.now()-datetime.timedelta(hours = hours),&quot;$lte&quot;:datetime.datetime.now()},
                             ***&quot;Analysis.Result&quot;:&quot;Neutral&quot;***
                      }
              },
            {&quot;$group&quot;:{&quot;_id&quot;:&quot;$Ticker&quot;, &quot;count&quot; : {&quot;$sum&quot; : 1}}}
           ]
  </code></pre>
  <p>it doesn't look so good. How can i do?</p>