title: How to unwind an array field which further contains an array
body: <p>Hi i have an <code>userAnswer schema</code> which looks like this</p>
  
  <pre><code>var userAnswer = mongoose.Schema({
  'questionId' : { 'type' : mongoose.Schema.Types.ObjectId, 'ref': 'Question'},
  'userId' : { 'type': mongoose.Schema.Types.ObjectId, 'ref' : 'User'},
  'score' : 1 
  })
  </code></pre>
  
  <p>Then an <code>userAttempt schema</code> which contains the attempts regarding to this question</p>
  
  <pre><code>var userAttempt = mongoose.Schema({
  'userAnswerId' : { 'type' : mongoose.Schema.Types.ObjectId, 'ref' : 'UserAnswer'},
  'attemptData' : [{ 
      'userInputMCQ' : String,
      'time' : Date 
      }],
  })
  </code></pre>
  
  <p>And last <code>usersnapshots schema</code> which saves snapshots regarding for each element in <code>attemptData</code> field of <code>userAttempt Schema</code></p>
  
  <p>it looks like this </p>
  
  <pre><code>var userAttemptSnapshot= mongoose.Schema({
  userAttemptDataId : {type: mongoose.Schema.Types.ObjectId },
  count : {type: Number, default: 1},
  userData :[ {
      value: String,
      timeOfCreation : { type : Date, default : Date.now }
      }]
  }, 
     {    timestamps: true    }
  );
  </code></pre>
  
  <p>Now i want to fetch all <code>data</code> regarding to a <code>question</code> attempt in such a way that all <code>documents</code> in <code>attempData</code> field of <code>UserAttempt Schema</code>
  which is </p>
  
  <pre><code> 'attemptData' : [{ 
      'userInputMCQ' : String,
      'time' : Date 
      }],
  </code></pre>
  
  <p>fetched as individual <code>attempt</code> with their respective <code>snapshots</code> using <code>aggregation</code></p>
  
  <p>My <code>aggregation query</code> is </p>
  
  <pre><code>db.useranswers.aggregate([ 
  { $match : {  'questionId' : ObjectId("5a2dfefd6dc3de029e488961"),'userId' :ObjectId("5a2cea6d6dc3de029e488814") }},
  { '$lookup' : {from: "userattempts",
      localField: "_id",
      foreignField: "userAnswerId",
      as: "attempts"}},
  { '$unwind' : '$attempts'},
  {'$unwind' : '$attempts.attemptData'}
  //after this stage i will use lookup to get snapshot data but i dont get those attempts as individual documents so that i could use lookup here
  })
  </code></pre>
  
  <p><em>Edit:</em>
  My <code>userAnswer</code> collection is</p>
  
  <pre><code>{ 
      "_id":ObjectId("5b4f359244781a45bcc98fcb") , 
      "userId":ObjectId("5a2cea6d6dc3de029e488814"),
      "questionId": ObjectId("5a631937e2509a3288171c37"),
  }
  </code></pre>
  
  <p><code>UserAttempt</code> collection is</p>
  
  <pre><code>{
      "_id":ObjectId("5b4f359204a7251ebcabef24"),
      "attemptData":[
      {
          "_id" : ObjectId("5b4f359204a7251ebcabef23"),
          "userInputMCQ" : "0"
          "timeOfCreation" : ISODate("2018-07-18T18:11:54.855+05:30"),
      },
      {
          "_id" : ObjectId("5b4f35bb04a7251ebcabef27"),
          "userInputMCQ" : "0",
          "timeOfCreation" : ISODate("2018-07-18T18:12:35.765+05:30"),
      } ]
  }
  
  UserAttemptSnapshot collection is
  {
      "_id":ObjectId("5b4f359204a7251ebcabef25"),
      "userAttemptId":ObjectId("5b4f359204a7251ebcabef23"),
      "count" : 2,
      "userData" : [ { 
                       'value' : "Some String" ,
                       "_id":ObjectId("5b4f359204a7251ebcabef26")  
                     },
                     { 
                       'value' : "Some String" ,
                       "_id": ObjectId("5b4f359204a7251ebcabef27")  
                     } ]
  },
  {
      "_id":ObjectId("5b4f359204a7251ebcabef25"),
      "userAttemptId":ObjectId("5b4f359204a7251ebcabef27"),
      "count" : 1,
      "userData" : [ { 
                       'value' : "Some String" ,
                       "_id":ObjectId("5b4f359204a7251ebcabeaee")  
                     },
                   ]
  }
  </code></pre>
  
  <p>I want output like this</p>
  
  <pre><code>{
       "_id":ObjectId("5b4f359244781a45bcc98fcb") , 
      "userId":ObjectId("5a2cea6d6dc3de029e488814"),
      "questionId": ObjectId("5a631937e2509a3288171c37"),
      "attemptData": {
          "_id" : ObjectId("5b4f359204a7251ebcabef23"),
          "userInputMCQ" : "0"
          "timeOfCreation" : ISODate("2018-07-18T18:11:54.855+05:30"),
          "userAttemptId" : ObjectId("5b4f359204a7251ebcabef23"),
          "count" : 2,
          "userData" : [ { 
                   'value' : "Some String" ,
                   "_id":ObjectId("5b4f359204a7251ebcabef26")  
                 },
                 { 
                   'value' : "Some String" ,
                   "_id": ObjectId("5b4f359204a7251ebcabef27")  
                 } ]
      },
  },
  {
       "_id":ObjectId("5b4f359244781a45bcc98fcb") , 
      "userId":ObjectId("5a2cea6d6dc3de029e488814"),
      "questionId": ObjectId("5a631937e2509a3288171c37"),
      "attemptData": {
            "_id" : ObjectId("5b4f35bb04a7251ebcabef27"),
            "userInputMCQ" : "0",
            "timeOfCreation" : ISODate("2018-07-18T18:12:35.765+05:30"),
            "userAttemptId":ObjectId("5b4f359204a7251ebcabef27"),
            "count" : 1,
            "userData" : [ { 
                   'value' : "Some String" ,
                   "_id":ObjectId("5b4f359204a7251ebcabeaee")  
                 },
               ]
      }  
  }
  </code></pre>