title: Convert array of objects to array of strings in mongodb
body: <p>I am looking at the following <a href="https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#std-label-unwind-example" rel="nofollow noreferrer">documentation</a>.</p>
  <p>The following inserts documents into a collection <code>classes</code>.</p>
  <pre><code>db.classes.insertMany( [
     { _id: 1, title: &quot;Reading is ...&quot;, enrollmentlist: [ &quot;giraffe2&quot;, &quot;pandabear&quot;, &quot;artie&quot; ], days: [&quot;M&quot;, &quot;W&quot;, &quot;F&quot;] },
     { _id: 2, title: &quot;But Writing ...&quot;, enrollmentlist: [ &quot;giraffe1&quot;, &quot;artie&quot; ], days: [&quot;T&quot;, &quot;F&quot;] }
  ] )
  </code></pre>
  <p>And the <code>members</code> collection:</p>
  <pre><code>db.members.insertMany( [
     { _id: 1, name: &quot;artie&quot;, joined: new Date(&quot;2016-05-01&quot;), status: &quot;A&quot; },
     { _id: 2, name: &quot;giraffe&quot;, joined: new Date(&quot;2017-05-01&quot;), status: &quot;D&quot; },
     { _id: 3, name: &quot;giraffe1&quot;, joined: new Date(&quot;2017-10-01&quot;), status: &quot;A&quot; },
     { _id: 4, name: &quot;panda&quot;, joined: new Date(&quot;2018-10-11&quot;), status: &quot;A&quot; },
     { _id: 5, name: &quot;pandabear&quot;, joined: new Date(&quot;2018-12-01&quot;), status: &quot;A&quot; },
     { _id: 6, name: &quot;giraffe2&quot;, joined: new Date(&quot;2018-12-01&quot;), status: &quot;D&quot; }
  ] )
  </code></pre>
  <p>They use the following aggregation to join the two collections on the array field, <code>enrollmentlist</code>.</p>
  <pre><code>db.classes.aggregate( [
     {
        $lookup:
           {
              from: &quot;members&quot;,
              localField: &quot;enrollmentlist&quot;,
              foreignField: &quot;name&quot;,
              as: &quot;enrollee_info&quot;
          }
     }
  ] )
  </code></pre>
  <p>Which returns the following:</p>
  <pre><code>{
     &quot;_id&quot; : 1,
     &quot;title&quot; : &quot;Reading is ...&quot;,
     &quot;enrollmentlist&quot; : [ &quot;giraffe2&quot;, &quot;pandabear&quot;, &quot;artie&quot; ],
     &quot;days&quot; : [ &quot;M&quot;, &quot;W&quot;, &quot;F&quot; ],
     &quot;enrollee_info&quot; : [
        { &quot;_id&quot; : 1, &quot;name&quot; : &quot;artie&quot;, &quot;joined&quot; : ISODate(&quot;2016-05-01T00:00:00Z&quot;), &quot;status&quot; : &quot;A&quot; },
        { &quot;_id&quot; : 5, &quot;name&quot; : &quot;pandabear&quot;, &quot;joined&quot; : ISODate(&quot;2018-12-01T00:00:00Z&quot;), &quot;status&quot; : &quot;A&quot; },
        { &quot;_id&quot; : 6, &quot;name&quot; : &quot;giraffe2&quot;, &quot;joined&quot; : ISODate(&quot;2018-12-01T00:00:00Z&quot;), &quot;status&quot; : &quot;D&quot; }
     ]
  }
  {
     &quot;_id&quot; : 2,
     &quot;title&quot; : &quot;But Writing ...&quot;,
     &quot;enrollmentlist&quot; : [ &quot;giraffe1&quot;, &quot;artie&quot; ],
     &quot;days&quot; : [ &quot;T&quot;, &quot;F&quot; ],
     &quot;enrollee_info&quot; : [
        { &quot;_id&quot; : 1, &quot;name&quot; : &quot;artie&quot;, &quot;joined&quot; : ISODate(&quot;2016-05-01T00:00:00Z&quot;), &quot;status&quot; : &quot;A&quot; },
        { &quot;_id&quot; : 3, &quot;name&quot; : &quot;giraffe1&quot;, &quot;joined&quot; : ISODate(&quot;2017-10-01T00:00:00Z&quot;), &quot;status&quot; : &quot;A&quot; }
     ]
  }
  </code></pre>
  <p>How can I reduce <code>enrolle_info</code> just to be an array of strings with all of the <code>name</code>s?</p>
  <p>This is the result I am after:</p>
  <pre><code>{
     &quot;_id&quot; : 1,
     &quot;title&quot; : &quot;Reading is ...&quot;,
     &quot;enrollmentlist&quot; : [ &quot;giraffe2&quot;, &quot;pandabear&quot;, &quot;artie&quot; ],
     &quot;days&quot; : [ &quot;M&quot;, &quot;W&quot;, &quot;F&quot; ],
     &quot;enrollee_info&quot; : [
        &quot;artie&quot;,
        &quot;pandabear&quot;
        &quot;giraffe2&quot;
     ]
  }
  {
     &quot;_id&quot; : 2,
     &quot;title&quot; : &quot;But Writing ...&quot;,
     &quot;enrollmentlist&quot; : [ &quot;giraffe1&quot;, &quot;artie&quot; ],
     &quot;days&quot; : [ &quot;T&quot;, &quot;F&quot; ],
     &quot;enrollee_info&quot; : [
        &quot;artie&quot;,
        &quot;giraffe1&quot;
     ]
  }
  </code></pre>
  <p>I have also looked into using multiple joins by introducing the <code>pipeline</code> field inside the <code>$lookup</code> operation. I am able to use a <code>$project</code> to get an array just with <code>{&quot;name&quot;: &quot;example&quot;}</code> but I am not sure how to remove the <code>&quot;name&quot;</code>. I have tried using an <code>{&quot;$unwind&quot;: &quot;$enrollee_info.name&quot;}</code> but that does not give me what I want. Do I need to introduce another stage to my aggregation pipeline <em>after</em> I do the join?</p>