title: $lookup for nested collection in mongodb
body: <p>Here I am looking to join nested joins with multiple collections: </p>
  
  <p>Below are the schemas of three collections ie USER, POST, and COMMENTS </p>
  
  <pre><code> const Users = new mongoose.Schema ({
        firstName: {type: String, default: null},
        lastName: {type: String, default: null},
        city:{type: String, default: null},
        country:{type: String, default: null},
        registerTime: {type: Date, default: Date.now},
        email: {type: String, required: true, unique: true},
        password: {type: String, default: null},
        token: {type: String, default: null},
        friends: [{status: String, userid: mongoose.Schema.Types.ObjectId, ftype:String}],
        userId:{type: String, default: null}
  
      });
  
  
  const PostSchema = new mongoose.Schema ({
  
      title: {type: String},
      body: {type: String},
      date: {type: Date,default: Date.now},
      tags:{type: String},
      flag:{type:String},
      _author: {type: mongoose.Schema.Types.ObjectId}
  
  });
  
  const CommentSchema = new mongoose.Schema ({
      comment: {type: String},
      postby: {type: mongoose.Schema.Types.ObjectId},
      date: {type: Date,default: Date.now},
      postid:{type:mongoose.Schema.Types.ObjectId}
  
  });
  </code></pre>
  
  <blockquote>
    <p>I have created below Query to get lists of all Posts with user details
     (join from the user) who posted that post.  --Done</p>
    
    <p>Get all Comments specific to each post -- Done</p>
  </blockquote>
  
  <p>Up to here, I am successfully able to make Left join for two the documents.I am using the following query and I get the result. </p>
  
  <pre><code>Posts.aggregate([  
                  {"$match": {'_flag': "PUBLISHED"}} ,
                  { $sort: { date: -1 } },
                  { $lookup: { 
                               from: 'users',
                               localField: '_author',
                               foreignField:'_id', 
                               as:'userDetail'
                              }
                  }, // post+ user document
  
                  { $lookup: {
                              from: 'comments', 
                              localField: '_id',
                              foreignField:'postid',
                              as:'commentdata'
                              }
                  }  // post+ comments
  ])
  </code></pre>
  
  <p>Here The result is :</p>
  
  <pre><code>{
  "status": "Post Listing",
      "posts": [{
          "_id": "5aae21fe58658522243ba9e3",
          "title": "hello zulias;s",
          "tags": "dsadas",
          "flag": "p",
          "_author": "87fe4052dd86c409576fe3e8bc8e6734a024e34d1ee4e841",
          "body": "&lt;p&gt;dsadasda&lt;/p&gt;\n",
          "date": "2018-03-18T08:23:26.870Z",
          "__v": 0,
          "userDetail":[{
              "fname": "anurag",
              "lname": "sads",
              "email": "zudsadalia@gmail.com"
          }],
  
          "commentdata": [{
              "_id": "5aafc8046aa1ec34ac7d8163",
              "postby": "5a993b12303c86133437c681",
              "postid": "5aae21fe58658522243ba9e3",
              "comment": "commment first, ",
              "date": "2018-03-19T14:24:04.231Z",
              "__v": 0
          },
          {
              "_id": "5aafc8076aa1ec34ac7d8164",
              "postby": "5a993b12303c86133437c681",
              "postid": "5aae21fe58658522243ba9e3",
              "comment": "commment first, \nCom ",
              "date": "2018-03-19T14:24:07.760Z",
              "__v": 0
          },
           {
              "_id": "5ab26b07d256913bf8de1cc4",
              "postby": "5a993b2e303c86133437c682",
              "postid": "5aae21fe58658522243ba9e3",
              "comment": "Dasdas",
              "date": "2018-03-21T14:24:07.176Z",
              "__v": 0
          }],
  
      },
  
      {
          "_id": "5aad5f207ecf7842bc4ab0f2",
          "title": "sdas",
          "tags": "dsad",
          "flag": "p",
          "_author": "87fe4052dd86c75e576fe3e8bc8e6734a024e34d1ee4e842",
          "body": "&lt;p&gt;sda&lt;/p&gt;\n",
          "date": "2018-03-17T18:32:00.194Z",
          "__v": 0,
          "commentdata": [],
          "userDetail":[{
              "fname": "zulia",
              "lname": "Rai",
              "email": "zulia@gmail.com"
          }],
      },
  
  }
  </code></pre>
  
  <p>The issue is, in above result, each post have <strong>Commentdata</strong> array in which all comments are there for a perticular post. Each comment inside <strong>Commentdata</strong> is associated with the single user who posted that comment. So here I want Username from user table for each comment.<br>
      ie </p>
  
  <pre><code>"commentdata": [{
                  "_id": "5aafc8046aa1ec34ac7d8163",
                  "postby": "5a993b12303c86133437c681",  // need name of this user from user table
                  "postid": "5aae21fe58658522243ba9e3",
                  "comment": "commment first, ",
                  "date": "2018-03-19T14:24:04.231Z",
                  "__v": 0,
                  fname": "zulia", // added after join 
              },
  </code></pre>