title: How do a merge multiple fields into a single array in an aggregation?
body: <p>I have data that has user primary keys in <code>&quot;user.pk&quot;</code>, and <code>&quot;usertags.user.pk&quot;</code>.</p>
  <pre><code>[
    {
      _id: ObjectId(&quot;62015250cb18d3cadb32c38e&quot;),
      user: { pk: '7485070525' },
      usertags: [ { user: { pk: '334777488' } } ]
    },
    {
      _id: ObjectId(&quot;62015250cb18d3cadb32c38f&quot;),
      user: { pk: '334777488' },
      usertags: [
        { user: { pk: '7485070525' } },
        { user: { pk: '271738610' } },
        { user: { pk: '31961021' } }
      ]
    }
  ]
  </code></pre>
  <p>I am looking to aggregate all this data into a single list of all distinct user primary keys, so the result for the data above would be something like <code>{ _id: null, user_pks: [7485070525, 334777488, 271738610, 31961021] }</code></p>
  <p>I tried using the <code>$group</code> aggregation, but I can't figure out how to merge <code>&quot;user.pk&quot;</code>, and <code>&quot;usertags.user.pk&quot;</code>. Can anyone help me with that?</p>
  <p>Edit:
  This is the closest I've gotten, which puts users and usertags in separate lists.</p>
  <pre><code>db.collection.aggregate([
    { $unwind: &quot;$usertags&quot; },
    { $group: { _id: null, users: { $push: &quot;$user.pk&quot; }, usertags: {$push: &quot;$usertags.user.pk&quot; } } },
  ])
  </code></pre>