title: Pymongo Aggregation with multi-fields
body: <p>I'm trying to aggregate using Pymongo with multiple fields, but I haven't found a way to get the correct value.</p>
  
  <p>I need to get count of total files by 2 fields: <code>time</code> and <code>state</code></p>
  
  <p><code>time</code> is a datetime object through pymongo, and I was able to get only yy/mm/dd using</p>
  
  <pre><code>'$group': { "_id":{
              "date":{"$concat": [
                     {"$substr": [{"$year": "$date"}, 0, 4 ]},
                     "-",
                     {"$substr": [{"$month": "$date"}, 0, 2 ]},
                     "-",
                     {"$substr": [{"$dayOfMonth": "$date"}, 0, 2 ]},
                ]}},
               "count":{"$sum": 1}}
  </code></pre>
  
  <p>This can get extract the correct date for me and count the number of logs, but now I need to also group by <code>state</code> so it becomes <code>GROUP BY date, state</code> in mysql</p>
  
  <p>I tried adding it inside <code>_id</code></p>
  
  <pre><code>'$group': { "_id":{
              "date":{"$concat": [
                     {"$substr": [{"$year": "$date"}, 0, 4 ]},
                     "-",
                     {"$substr": [{"$month": "$date"}, 0, 2 ]},
                     "-",
                     {"$substr": [{"$dayOfMonth": "$date"}, 0, 2 ]},
                ]}},
               "_id":{"state":"$timeline.state"},
               "count":{"$sum": 1}}
  </code></pre>
  
  <p>and it creates multiple outputs in the format like <code>{u'count': 4111, u'_id': {u'state': [0, 1]}}</code>, the 0 and 1 are codes for different states. The date is nowhere to be found.</p>
  
  <p>I then tried </p>
  
  <pre><code>'$group': { "_id":{
              "date":{"$concat": [
                     {"$substr": [{"$year": "$date"}, 0, 4 ]},
                     "-",
                     {"$substr": [{"$month": "$date"}, 0, 2 ]},
                     "-",
                     {"$substr": [{"$dayOfMonth": "$date"}, 0, 2 ]},
                ]}},
               "state":"$timeline.state"},
               "count":{"$sum": 1}}
  </code></pre>
  
  <p>and I got <code>failed: A pipeline stage specification object must contain exactly one field.</code></p>
  
  <p>This looks like I didn't get the brackets at the right place, but no matter how I change the format, the same error remains. Now I'm wondering if it's really the bracket's problem. Most importantly, how do I do it correctly?</p>