title: How to lookup a field with an array in nested subdocument mongodb?
body: <p>I am trying to retrieve some lookup data for an embedded array in a document.</p>
  
  <p>Here is a sample of the data:</p>
  
  <pre><code>{
        "_id": "58a4fa0e24180825b05e14e9",
        "fullname": "Test User",
        "username": "testuser"
        "teamInfo": {
            "challenges": [
                {
                    "levelId": "5e14e958a4fa0",
                    "title": "test challenge 1.1"
                },
                {
                    "levelId": "5e14e958a4fa0",
                    "title": "test challenge 1.2"
                },
                {
                    "levelId": "5e14e958a4fa1",
                    "title": "test challenge 2.1"
                }
            ]
        }
  }
  </code></pre>
  
  <p>As you see, teamInfo.challenges is an array, containing levelId fields. These are pointing to the _id field in another collection called levels.</p>
  
  <p>But how can I do to getting json response like this?</p>
  
  <pre><code>{
        "_id": "58a4fa0e24180825b05e14e9",
        "fullname": "Test User",
        "username": "testuser"
        "teamInfo": {
            "challenges": [
                {
                    "levelInfo": {
                        "name": "Level 1"
                    },
                    "title": "test challenge 1.1"
                },
                {
                    "levelInfo": {
                        "name": "Level 1"
                    },
                    "title": "test challenge 1.2"
                },
                {
                    "levelInfo": {
                        "name": "Level 2"
                    },
                    "title": "test challenge 2.1"
                }
            ]
        }
  }
  </code></pre>
  
  <p>Im trying using unwind, project, and group. But im so confused.</p>
  
  <pre><code>const user = await User.aggregate([
                  {
                      $match: {_id: new mongoose.Types.ObjectId(req.user.userId)}
                  },
                  {
                      $lookup: {
                          from: 'levels',
                          localField: 'teamInfo.challenges.levelId',
                          foreignField: '_id',
                          as: 'challLevelInfo'
                      }
                  },
                  {
                      $group: {
                          _id: "$_id",
                          ........IM CONFUSED HERE........
                      }
                  }
  ]);
  </code></pre>