title: $lookup to Join within Nested Array elements
body: <p>I have two collections <code>SystemFeatures</code> and <code>SystemRoleFeatures</code></p>
  
  <p><code>SystemFeatures</code> schema has following record </p>
  
  <pre><code>{
      "_id" : ObjectId("5a97af5ec442b59373b8dff3"),
      "Name" : "Manage Operation",
      "Description" : "Manage the Operations Details.",
      "ChildFeatures" : [{
              "_id" : ObjectId("5a97b084c442b59373b8e004"),
              "Name" : "Add Operation",
              "Description" : "Add Operation",
              "ParentId" : ObjectId("5a97af5ec442b59373b8dff3")
          }, {
              "_id" : ObjectId("5a97b084c442b59373b8e005"),
              "Name" : "Edit Operation",
              "Description" : "Edit Operation",
              "ParentId" : ObjectId("5a97af5ec442b59373b8dff3")
          }
      ]
  }{
      "_id" : ObjectId("5a97af5ec442b59373b8dff4"),
      "Name" : "Manage User Roles",
      "Description" : "Manage the Functional Roles Details.",
      "ChildFeatures" : [{
              "_id" : ObjectId("5a97b084c442b59373b8e006"),
              "Name" : "Add role",
              "Description" : "Add role",
              "ParentId" : ObjectId("5a97af5ec442b59373b8dff4")
          }, {
              "_id" : ObjectId("5a97b084c442b59373b8e007"),
              "Name" : "Edit role",
              "Description" : "Edit role",
              "ParentId" : ObjectId("5a97af5ec442b59373b8dff4")
          }
      ]
  }{..}
  </code></pre>
  
  <p><code>SystemRoleFeatures</code> schema has following record</p>
  
  <pre><code>    {
          "_id" : ObjectId("5a9fd2d0b1c4f30e432fd5e6"),
          "SystemRoleId" : ObjectId("5a9f9583e8c0dc6b0c5456c9"),
          "SystemFeatures" : [{
                  "SystemFeatureId" : ObjectId("5a97af5ec442b59373b8dff3"),
                  "Isdefault" : true,
                  "ChildFeatures" : [{
                          "SystemFeatureId" : ObjectId("5a97b084c442b59373b8e004"),
                          "Isdefault" : true
                      }, {
                          "SystemFeatureId" : ObjectId("5a97b084c442b59373b8e005"),
                          "Isdefault" : true
                      }
                  ]
              }, {
                  "SystemFeatureId" : ObjectId("5a97af5ec442b59373b8dff4"),
                  "Isdefault" : true,
                  "ChildFeatures" : [{
                          "SystemFeatureId" : ObjectId("5a97b084c442b59373b8e006"),
                          "Isdefault" : true
                      }, {
                          "SystemFeatureId" : ObjectId("5a97b084c442b59373b8e007"),
                          "Isdefault" : true
                      }
                  ]
              }, {
                  "SystemFeatureId" : ObjectId("5a97af5ec442b59373b8dff5"),
                  "Isdefault" : true,
                  "ChildFeatures" : [{
                          "SystemFeatureId" : ObjectId("5a97b084c442b59373b8e008"),
                          "Isdefault" : true
                      }, {
                          "SystemFeatureId" : ObjectId("5a97b084c442b59373b8e009"),
                          "Isdefault" : true
                      }
                  ]
              }, {
                  "SystemFeatureId" : ObjectId("5a97b084c442b59373b8e018"),
                  "Isdefault" : true,
                  "ChildFeatures" : []
              }
          ]
      }
  </code></pre>
  
  <p>I want output be like as below</p>
  
  <pre><code>{
          "_id" : ObjectId("5a9fd2d0b1c4f30e432fd5e6"),
          "SystemRoleId" : ObjectId("5a9f9583e8c0dc6b0c5456c9"), // i'm passing this systemroleid based on this this aggreration is done
          "SystemFeatures" : [{
                  "SystemFeatureId" : ObjectId("5a97af5ec442b59373b8dff3"),
                  "Name" : "Manage Operation",
                  "Description" : "Manage the Operations Details.",
                  "Isdefault" : true,
                  "ParentId" : null, // if there is no parentId than it should be null
                  "ChildFeatures" : [{
                          "SystemFeatureId" : ObjectId("5a97b084c442b59373b8e004"),
                          "Name" : "Add Operation", //these detail from systemfeature
                          "Description" : "Add Operation",
                          "ParentId" : ObjectId("5a97af5ec442b59373b8dff3")
                          "Isdefault" : true
                      }, {
                          "SystemFeatureId" : ObjectId("5a97b084c442b59373b8e005"),
                          "Name" : "Edit Operation",
                          "Description" : "Edit Operation",
                          "ParentId" : ObjectId("5a97af5ec442b59373b8dff3")
                          "Isdefault" : true,
                      }
                  ]
              }, {
                  "SystemFeatureId" : ObjectId("5a97af5ec442b59373b8dff4"),
                  "Isdefault" : true,
                  "Name" : "Manage User Roles",
                  "Description" : "Manage the Functional Roles Details.",
                  "ChildFeatures" : [{
                          "SystemFeatureId" : ObjectId("5a97b084c442b59373b8e006"),
                          "Name" : "Add role",
                          "Description" : "Add role",
                          "ParentId" : ObjectId("5a97af5ec442b59373b8dff4")
                          "Isdefault" : true
                      }, {
                          "SystemFeatureId" : ObjectId("5a97b084c442b59373b8e007"),
                          "Name" : "Edit role",
                          "Description" : "Edit role",
                          "ParentId" : ObjectId("5a97af5ec442b59373b8dff4")
                          "Isdefault" : true
                      }
                  ]
              }, {
                  "SystemFeatureId" : ObjectId("5a97af5ec442b59373b8dff5"),
                  // same from systemfeature document
                  "Isdefault" : true,
                  "ChildFeatures" : [{
                          "SystemFeatureId" : ObjectId("5a97b084c442b59373b8e008"),
                          // same from systemfeature document
                          "Isdefault" : true
                      }, {
                          "SystemFeatureId" : ObjectId("5a97b084c442b59373b8e009"),
                          // same from systemfeature document
                          "Isdefault" : true
                      }
                  ]
              }, {
                  "SystemFeatureId" : ObjectId("5a97b084c442b59373b8e018"),
                  // same from systemfeature document
                  "Isdefault" : true,
                  "ChildFeatures" : []
              }
          ]
      }
  </code></pre>
  
  <p>For this i'm doing this stuff,</p>
  
  <pre><code>db.SystemRoleFeatures.aggregate([{
                  "$match" : {
                      "SystemRoleId" : ObjectId("5a9f9583e8c0dc6b0c5456c9")
                  }
              }, {
                  "$addFields" : {
                      "join" : {
                          "$reduce" : {
                              "input" : "$SystemFeatures.ChildFeatures.SystemFeatureId",
                              "initialValue" : [],
                              "in" : {
                                  "$concatArrays" : ["$$value", "$$this"]
                              }
                          }
                      }
                  }
              }, {
                  "$lookup" : {
                      "from" : "SystemFeatures",
                      "localField" : "join",
                      "foreignField" : "_id",
                      "as" : "join"
                  }
              }
          ]).pretty()
  </code></pre>
  
  <p>but,some how it not return any result,i know its not working here,because in both<br>
  document there are array. That's why it return null.</p>