title: Formatting the returned object from MongoDB/Mongoose group by
body: <p>I have a MongoDB with documents of the form:</p>
  
  <pre><code>{
      ...
      "template" : "templates/Template1.html",
      ...
  }
  </code></pre>
  
  <p>where <code>template</code> is either <code>"templates/Template1.html"</code>, <code>"templates/Template2.html"</code> or <code>"templates/Template3.html"</code>.</p>
  
  <p>I'm using this query to group by <code>template</code> and count how many times each <code>template</code> is used:</p>
  
  <pre><code>var group = {
          key:{'template':1},
          reduce: function(curr, result){ result.count++ },
          initial: { count: 0 }
      };
  
      messageModel.collection.group(group.key, null, group.initial, group.reduce, null, true, cb);
  </code></pre>
  
  <p>I'm getting back the correct result, but it's formatted like this:</p>
  
  <pre><code>{
      "0" : {
          "template" : "templates/Template1.html",
          "count" : 2 },
      "1" : {
          "template" : "templates/Template2.html",
          "count" : 2 },
      "2" : {
          "template" : "templates/Template3.html",
           "count" : 1 }
  }
  </code></pre>
  
  <p>I was wondering if it's possible to change the query so that it returns something like:</p>
  
  <pre><code>{
      "templates/Template1.html" : { "count" : 2 },
      "templates/Template2.html" : { "count" : 2 },
      "templates/Template3.html" : { "count" : 1 }
  }
  </code></pre>
  
  <p>or even:</p>
  
  <pre><code>{
      "templates/Template1.html" : 2 ,
      "templates/Template2.html" : 2 ,
      "templates/Template3.html" : 1
  }
  </code></pre>
  
  <p>I would rather change the query and not parse the returned object from the original query.</p>