title: Populate one collection with lookup in mongoose
body: <p>I'm trying to populate a collection with lookup. The thing is I need to join three collections at once using mongoose in express js.
  I have three collections namely, <strong>users, skills, userskills</strong>. </p>
  
  <p>User and UserSkills are connected. Skills and UserSkills are connected. But not User and Skills.</p>
  
  <p>My model is like</p>
  
  <p><strong>users</strong></p>
  
  <pre><code>{ 
      _id: 5ec6d940b98e8f2c3cea5f22, 
      name: "test one", 
      email: "test@example.com" 
  }
  </code></pre>
  
  <p><strong>skills</strong></p>
  
  <pre><code>{ 
      _id: 5ec786b21cea7d8c8c186a54, 
      title: "java" 
  }
  </code></pre>
  
  <p><strong>user-skills</strong></p>
  
  <pre><code>{
      _id: 5ec7879c1cea7d8c8c186a56,
      skillId: "5ec786b21cea7d8c8c186a54",
      userId: "5ec6d940b98e8f2c3cea5f22", 
      level: "good" 
  }
  </code></pre>
  
  <p>I tried </p>
  
  <pre><code>user = await User.aggregate([{
          $lookup: {
            from: "userskills",
            localField: "_id",
            foreignField: "userId",
            as: "userskills"
          }
        }, {
          $unwind: {
            path: "$userskills",
            preserveNullAndEmptyArrays: true
          }
        }, {
          $lookup: {
            from: "skills",
            localField: "userskills.skillId",
            foreignField: "_id",
            as: "userskills.skill",
          }
        }, {
          $group: {
            _id : "$_id",
            name: { $first: "$name" },
            skill: { $push: "$skills" }
          }
        }, {
          $project: {
            _id: 1,
            name: 1,
            skills: {
              $filter: { input: "$skills", as: "a", cond: { $ifNull: ["$$a._id", false] } }
            } 
          }
        }]);
  </code></pre>
  
  <p>Required result:</p>
  
  <pre><code>{
      "users" : [
          {
              _id: "5ec6d940b98e8f2c3cea5f22"
              name: "test one",
              email: "testone@example.com",
              "skills" : [
                  {
                      _id: "5ec7879c1cea7d8c8c186a56",
                      level: "good",
                      "skill" : {
                           _id: "5ec786b21cea7d8c8c186a54",
                           title: "java"
                      }
                  }
             ]
          },
          {
              _id: "5ec6d940b98e8f2c3cea5f23"
              name: "test two",
              email: "testtwo@example.com",
              "skills" : [
                  {
                      _id: "5ec7879c1cea7d8c8c186a57",
                      level: "good",
                      "skill" : {
                           _id: "5ec786b21cea7d8c8c186a55",
                           title: "php"
                      }
                  }
             ]
          }
      ]
  }
  </code></pre>
  
  <p>when using</p>
  
  <pre><code>user = await User.find().populate('Skills").exec()
  </code></pre>
  
  <p>The result is coming like</p>
  
  <pre><code>{
      "users" : [
          {
              _id: "5ec6d940b98e8f2c3cea5f22"
              name: "test one",
              email: "testone@example.com",
              "skills" : [
                  {
                      _id: "5ec7879c1cea7d8c8c186a56",
                      level: "good",
                      skillId: "5ec786b21cea7d8c8c186a55" 
                  }
             ]
          }
      ]
  }
  </code></pre>
  
  <p>The problem is I need the skill name should also be fetched. Please help me to solve this issue. I'm writing a backend API in nodejs and mongodb.</p>