title: MongoDB $lookup aggregation resulting in nested array
body: <p>I have two collections that are in many-to-one relationship (multiple hosts' http services often serve the 'same' e.g. DNS-level load balancing).
  I'm trying to build a query returning the relevant documents (from two collections) merged into one.</p>
  <p>hosts collection:</p>
  <pre><code>{
      &quot;_id&quot; : ObjectId(&quot;60aa2485332483cb4f5e7122&quot;),
      &quot;ip&quot; : &quot;1.2.3.4&quot;,
      &quot;services&quot; : [
          {
              &quot;proto&quot; : &quot;tcp&quot;,
              &quot;port&quot; : &quot;22&quot;,
              &quot;status&quot; : &quot;open&quot;,
              &quot;reason&quot; : &quot;syn-ack&quot;,
              &quot;ttl&quot; : 53,
          },
          {
              &quot;proto&quot; : &quot;tcp&quot;,
              &quot;port&quot; : &quot;80&quot;,
              &quot;status&quot; : &quot;open&quot;,
              &quot;reason&quot; : &quot;syn-ack&quot;,
              &quot;ttl&quot; : 51,
              &quot;http&quot; : [
                  ObjectId(&quot;60aa64c67d0bf23ce47c530c&quot;)
              ]
          }
      ],
      &quot;version&quot; : 4,
      &quot;last_scanned&quot; : 1621573240.730579,
  </code></pre>
  <p>https collection:</p>
  <pre><code>{
      &quot;_id&quot; : ObjectId(&quot;60aa64c67d0bf23ce47c530c&quot;),
      &quot;vhost&quot; : &quot;test.com&quot;,
      &quot;paths&quot; : [
          {
              &quot;path&quot; : &quot;/admin&quot;,
              &quot;code&quot; : 200
          },
          {
              &quot;path&quot; : &quot;/stuff&quot;,
              &quot;code&quot; : 200
          }
      ]
  }
  </code></pre>
  <p>I'd like to write a lookup where the output is a combination of these two collections. So far I was able to get the https document into a top-level array in hosts:</p>
  <pre><code>db.hosts.aggregate([                                                                                                                                       
    {                                                                             
      $lookup:                                                                    
          {                                                                       
              from: &quot;https&quot;,                                                      
              localField: &quot;services.http&quot;,                                        
              foreignField: &quot;_id&quot;,                                                
              as: 'http'                                                 
          }                                                                       
    }                                                                             
  ]).pretty()
  </code></pre>
  <p>Which ends up as:</p>
  <pre><code>{
      &quot;_id&quot; : ObjectId(&quot;60aa2485332483cb4f5e7122&quot;),
      &quot;ip&quot; : &quot;1.2.3.4&quot;,
      &quot;services&quot; : [
          {
              &quot;proto&quot; : &quot;tcp&quot;,
              &quot;port&quot; : &quot;22&quot;,
              &quot;status&quot; : &quot;open&quot;,
              &quot;reason&quot; : &quot;syn-ack&quot;,
              &quot;ttl&quot; : 53,
          },
          {
              &quot;proto&quot; : &quot;tcp&quot;,
              &quot;port&quot; : &quot;80&quot;,
              &quot;status&quot; : &quot;open&quot;,
              &quot;reason&quot; : &quot;syn-ack&quot;,
              &quot;ttl&quot; : 51,
              &quot;http&quot; : [
                  ObjectId(&quot;60aa64c67d0bf23ce47c530c&quot;)
              ]
          }
      ],
      &quot;http&quot; : [
          {
              &quot;_id&quot; : ObjectId(&quot;60aa64c67d0bf23ce47c530c&quot;),
              &quot;vhost&quot; : &quot;test.com&quot;,
              &quot;paths&quot; : [
                  {
                      &quot;path&quot; : &quot;/admin&quot;,
                      &quot;code&quot; : 200
                  },
                  {
                      &quot;path&quot; : &quot;/stuff&quot;,
                      &quot;code&quot; : 200
                  }
              ]
          }
      ]
      &quot;version&quot; : 4,
      &quot;last_scanned&quot; : 1621573240.730579
      ]
  }
  </code></pre>
  <p>The problem is that I can't move the &quot;http&quot; field to the place where it's ObjectId was found by lookup (services.$.http). I was trying to modify the 'as' field of $lookup in various ways without success.</p>
  <p>Is it even possible to point to lower levels of a nested document with 'as'?
  Any workaround to achieve this?</p>