title: MongoDB aggregation: Group all records into a single result
body: <p>In a mongodb aggregation call, how do I use the $group operator to group all documents in the pipeline into a single result?</p>
  
  <p>Let's say I have a collection of records that look like this:</p>
  
  <pre><code>{
      user_id: 234,
      signup_date: 2013-06-27
  }
  </code></pre>
  
  <p>I want to use the aggregate function to query the database for a list of users who signed up in a given date range, and return it as a list. I'd like a result that looks like this:</p>
  
  <pre><code>{
      users: [234, 235, 236]
  }
  </code></pre>
  
  <p>My query looks something like this:</p>
  
  <pre><code>db.users.aggregate([
  
      { $match: {
          signup_date: {
              $gte: date_begin_variable,
              $lt: date_end_variable
      }},
  </code></pre>
  
  <p>So far, so good. I now have a subset of records, all of which have a signup date in the desired range. But here's where I get into trouble. Now I want to group ALL those records into a single result containing a list of all the IDs. But I don't know how to do that using $group.</p>
  
  <pre><code>    { $group: {
          _id: null
          users: { $addToSet: user_id }
      }}
  
  ])
  </code></pre>
  
  <p>When I tried _id: null, I got back an empty list of user IDs.</p>
  
  <p>I could just return the desired set of records using find(), and then just process the IDs into a list manually, but doing it using aggregate() seems cleaner. Am I wrong about that? What would be the best way to accomplish what I'm describing, either using aggregate or not?</p>