title: Using mongodb aggregate, how to turn field values into array literal
body: <p>We're making a query where the results returned should be a list of suggested search terms.</p>
  
  <p>We currently have a query that checks for a regex match at multiple fields:</p>
  
  <pre><code>$or:[ 
  {'description.position':/s/i}, 
  {'employer.name':/s/i}, 
  {'hiringManager.profile.name':/s/i}
  ]
  </code></pre>
  
  <p>We'd like the returned results be an array of matches that unique (not duplicates).</p>
  
  <p>The results returned look something like:</p>
  
  <pre><code>I20150311-18:17:14.151(-7)?   "fields": {
  I20150311-18:17:14.154(-7)?     "hiringManager": {
  I20150311-18:17:14.157(-7)?       "profile": {
  I20150311-18:17:14.160(-7)?         "name": "Seth Sandler"
  I20150311-18:17:14.163(-7)?       }
  I20150311-18:17:14.167(-7)?     },
  I20150311-18:17:14.173(-7)?     "description": {
  I20150311-18:17:14.177(-7)?       "position": "Cook"
  I20150311-18:17:14.181(-7)?     },
  I20150311-18:17:14.187(-7)?     "employer": {
  I20150311-18:17:14.191(-7)?       "name": "Employer"
  I20150311-18:17:14.195(-7)?     },
  I20150311-18:17:14.206(-7)?   }
  I20150311-18:17:14.209(-7)? }
  I20150311-18:17:14.212(-7)? {
  I20150311-18:17:14.223(-7)?   "fields": {
  I20150311-18:17:14.226(-7)?     "hiringManager": {
  I20150311-18:17:14.229(-7)?       "profile": {
  I20150311-18:17:14.232(-7)?         "name": "Seth Sandler"
  I20150311-18:17:14.234(-7)?       }
  I20150311-18:17:14.237(-7)?     },
  I20150311-18:17:14.240(-7)?     "description": {
  I20150311-18:17:14.243(-7)?       "position": "Cook"
  I20150311-18:17:14.246(-7)?     },
  I20150311-18:17:14.249(-7)?     "employer": {
  I20150311-18:17:14.252(-7)?       "name": "Employer 4"
  I20150311-18:17:14.254(-7)?     },
  I20150311-18:17:14.264(-7)?   }
  I20150311-18:17:14.267(-7)? }
  I20150311-18:17:14.269(-7)? {
  I20150311-18:17:14.281(-7)?   "fields": {
  I20150311-18:17:14.284(-7)?     "hiringManager": {
  I20150311-18:17:14.287(-7)?       "profile": {
  I20150311-18:17:14.290(-7)?         "name": "Seth Sandler"
  I20150311-18:17:14.293(-7)?       }
  I20150311-18:17:14.295(-7)?     },
  I20150311-18:17:14.298(-7)?     "description": {
  I20150311-18:17:14.301(-7)?       "position": "Chef"
  I20150311-18:17:14.304(-7)?     },
  I20150311-18:17:14.307(-7)?     "employer": {
  I20150311-18:17:14.310(-7)?       "name": "Emplopyer 3"
  I20150311-18:17:14.313(-7)?     },
  I20150311-18:17:14.321(-7)?   }
  I20150311-18:17:14.323(-7)? }
  I20150311-18:17:14.325(-7)? {
  I20150311-18:17:14.334(-7)?   "fields": {
  I20150311-18:17:14.336(-7)?     "hiringManager": {
  I20150311-18:17:14.338(-7)?       "profile": {
  I20150311-18:17:14.340(-7)?         "name": "Seth Sandler"
  I20150311-18:17:14.342(-7)?       }
  I20150311-18:17:14.344(-7)?     },
  I20150311-18:17:14.346(-7)?     "description": {
  I20150311-18:17:14.348(-7)?       "position": "Chef"
  I20150311-18:17:14.350(-7)?     },
  I20150311-18:17:14.353(-7)?     "employer": {
  I20150311-18:17:14.356(-7)?       "name": "Employer"
  I20150311-18:17:14.359(-7)?     },
    I20150311-18:17:14.366(-7)?   }
  I20150311-18:17:14.369(-7)? }
  </code></pre>
  
  <p>We'd like to instead have the results be a unique array for values of hiringManager.profile.name, employer.name, and description.position.</p>
  
  <p>Our current solution doesn't seem ideal (probably not performant), and were wondering if it's possible using the mongogodb aggregate function to put field values into an array.</p>
  
  <p>Current solution (not ideal):</p>
  
  <pre><code>aggregate([
  {$match: {$or:[ {'description.position':/s/i}, {'employer.name':/s/i}, {'hiringManager.profile.name':/s/i}    ]}},
  {$group: {_id: 1, positions: {$push: '$description.position'}, employerNames: {$push: '$employer.name'}, hiringManagerNames: {$push:'$hiringManager.profile.name'}}},
  {$project: {_id:1, texts: {$setUnion: ['$positions', {$setUnion: ['$employerNames', '$hiringManagerNames']}]}}}
  ])
  })
  </code></pre>
  
  <p>The output of this is correct, but we'd like a better aggregate function where we can limit the results.</p>
  
  <pre><code>I20150311-18:25:26.461(-7)?   "result": [
  I20150311-18:25:26.465(-7)?     {
  I20150311-18:25:26.468(-7)?       "_id": 1,
  I20150311-18:25:26.472(-7)?       "texts": [
  I20150311-18:25:26.478(-7)?         "Employer 5",
  I20150311-18:25:26.481(-7)?         "Employer 4",
  I20150311-18:25:26.485(-7)?         "Employer 1",
  I20150311-18:25:26.488(-7)?         "Manager",
  I20150311-18:25:26.504(-7)?         "Cook",
  I20150311-18:25:26.507(-7)?         "Chef",
  I20150311-18:25:26.530(-7)?       ]
  I20150311-18:25:26.534(-7)?     }
  I20150311-18:25:26.538(-7)?   ]
  </code></pre>