title: How to join more than 2 collections in mongodb?
body: <p>I have got 3 collections: groups, users, and members.
  Groups collection contains group specific details,
  users collection contains user-specific details and members collection
  contains the association of users with groups.</p>
  <p>For example:</p>
  <pre><code>Groups:
  
  id                                      |   name 
  ObjectId(&quot;5ee5e346fae4a21e28a81d91&quot;)    |   Housemates 
  ObjectId(&quot;5ee5e346fae4a21e28a81d92&quot;)    |   Co-workers
  
  
  Users:
  
  id                                      |   name
  ObjectId(&quot;5ee493b0989d0f271cdc41c1&quot;)    |   Joulie
  ObjectId(&quot;5ee493b0989d0f271cdc41c3&quot;)    |   Newelle
  ObjectId(&quot;5ee493b0989d0f271cdc41c5&quot;)    |   John
  ObjectId(&quot;5ee493b0989d0f271cdc41c7&quot;)    |   Larry
  
  Members:
  group_id                                | user_id  
  ObjectId(&quot;5ee5e346fae4a21e28a81d91&quot;)    | ObjectId(&quot;5ee493b0989d0f271cdc41c1&quot;)
  ObjectId(&quot;5ee5e346fae4a21e28a81d91&quot;)    | ObjectId(&quot;5ee493b0989d0f271cdc41c3&quot;)
  ObjectId(&quot;5ee5e346fae4a21e28a81d92&quot;)    | ObjectId(&quot;5ee493b0989d0f271cdc41c5&quot;)
  ObjectId(&quot;5ee5e346fae4a21e28a81d92&quot;)    | ObjectId(&quot;5ee493b0989d0f271cdc41c7&quot;)
  </code></pre>
  <p>I want to join these three collections and get user details for each group with the group name.</p>
  <pre><code>   Expected Output:
      [
          { &quot;group_name&quot;:&quot;Housemates&quot;, 
            &quot;user_info&quot;: [
              {&quot;name&quot;:&quot;Joulie&quot;},
              {&quot;name&quot;:&quot;Newelle&quot;}
              ]
          },
          { &quot;group_name&quot;:&quot;Co-workers&quot;, 
            &quot;user_info&quot;: [
              {&quot;name&quot;:&quot;John&quot;},
              {&quot;name&quot;:&quot;Larry&quot;}
              ]
          }
     ]
  </code></pre>
  <p>I've written a query to get the output as above but it's not working:</p>
  <pre><code>db.members.aggregate([
        {
          $lookup : {
            from: 'users',
            localField: &quot;user_id&quot;,
            foreignField: &quot;_id&quot;,
            as: &quot;user_info&quot;
          }
        },{
          $lookup: {
              from: 'groups',
              localField: &quot;group_id&quot;,
              foreignField: &quot;_id&quot;,
              as: &quot;group_info&quot;
          }
        }
      ]);
  </code></pre>
  <p><a href="https://stackoverflow.com/questions/35813854/how-to-join-multiple-collections-with-lookup-in-mongodb">This</a> question looks similar and I have tried the solution from it as well, but it does not seem to work for me. I would really appreciate any help or guideline.
  Thank you in advance!</p>