title: MongoDB Aggregation - $group after $lookup + $unwind
body: <p>So I am new to the aggregation framework. I have a 2 collections with these documents:</p>
  
  <p><strong>users</strong></p>
  
  <pre class="lang-py prettyprint-override"><code>{
      '_id': ObjectId(user1),
      'name': 'Name1',
      'event': ObjectId(event1)
  },
  {
      '_id': ObjectId(user2),
      'name': 'Name2',
      'event': ObjectId(event2)
  },
  {
      '_id': ObjectId(user3),
      'name': 'Name3',
      'event': ObjectId(event1)
  }
  </code></pre>
  
  <p><strong>events</strong></p>
  
  <pre class="lang-py prettyprint-override"><code>{
      '_id': ObjectId(event1),
      'name': 'Event2',
      'type': 'Party'
  },
  {
      '_id': ObjectId(event2),
      'name': 'Event2',
      'type': 'Meeting'
  }
  </code></pre>
  
  <p><strong>Expected Output</strong></p>
  
  <pre class="lang-py prettyprint-override"><code>[
      {
      '_id': ObjectId(user1),
      'name': 'Name1',
      'event': 'Party'
      },
      {
      '_id': ObjectId(user2),
      'name': 'Name2',
      'event': 'Meeting'
      },
      {
      '_id': ObjectId(user3),
      'name': 'Name3',
      'event': 'Party'
      }
  
  ]
  </code></pre>
  
  <p>So as you can see, form the explanation above, I want to <strong>group</strong> users, and return users based on the event that they have attended. </p>
  
  <p><strong>what I have done so far</strong> (aggregating on users)</p>
  
  <pre class="lang-py prettyprint-override"><code>[
      {
          "$lookup":
          {
              "from": "events",
              "localField": "event",
              "foreignField": "_id",
              "as": "Events"
          }
      },
      {
          "$unwind": "$Events"
      },
      {
          "$group":
          {
              "_id":
              {
                  "type": "$Events.type"
              }
          }
      }
  ]
  </code></pre>
  
  <p>I am only getting the event documents. The output is:</p>
  
  <pre class="lang-py prettyprint-override"><code>{ 'type':'Party'},
  {'type': 'Meeting'}
  </code></pre>
  
  <blockquote>
    <p>Can someone please guide me as to how you can group documents in collection by a field from a document in another collection? </p>
  </blockquote>
  
  <p>Thankyou!</p>