title: How to group documents with specific field in aggregation of mongodb
body: <p>I have a collection of documents in mongodb like this:</p>
  
  <pre><code>{
      "_id" : ObjectId("558cd1c4bf46acde47a0d4f6"),
      "content" : "That is very good",
      "createTime" : NumberLong("1435292100235"),
      "deleted" : false,
      "parent" : "",
      "poiId" : "538318500cf2e33469a2d819",
      "poiType" : "event",
      "role" : "user",
      "threadId" : "e9f800dc3c6dca0b98604bc08ad5ffec17c2aba1",
      "thumbsUp" : 0,
      "userId" : "53a85640f66cce8abfdb433e5215c8d981d0df86"
  }
  {
      "_id" : ObjectId("558cd375bf46acde47a0d4f7"),
      "content" : "That is very good",
      "createTime" : NumberLong("1435292533764"),
      "deleted" : false,
      "parent" : "558cd1c4bf46acde47a0d4f6",
      "poiId" : "538318500cf2e33469a2d819",
      "poiType" : "event",
      "role" : "user",
      "threadId" : "e9f800dc3c6dca0b98604bc08ad5ffec17c2aba1",
      "thumbsUp" : 0,
      "userId" : "53a85640f66cce8abfdb433e5215c8d981d0df86"
  }
  {
      "_id" : ObjectId("558cd380bf46acde47a0d4f8"),
      "content" : "That is very good",
      "createTime" : NumberLong("1435292544172"),
      "deleted" : false,
      "parent" : "",
      "poiId" : "538318500cf2e33469a2d819",
      "poiType" : "event",
      "role" : "user",
      "threadId" : "cefd5af2429b42b3b97295db53369af6f1e033c8",
      "thumbsUp" : 0,
      "userId" : "53a85640f66cce8abfdb433e5215c8d981d0df86"
  }
  {
      "_id" : ObjectId("558cd3cfbf46f275306cb661"),
      "content" : "\"喲,看上去不错哦\"",
      "createTime" : NumberLong("1435292623005"),
      "deleted" : false,
      "parent" : "",
      "poiId" : "538318500cf2e33469a2d819",
      "poiType" : "event",
      "role" : "user",
      "threadId" : "18e453d0e4d42429d1a517c619da0eba4de8bc65",
      "thumbsUp" : 0,
      "userId" : "53a85640f66cce8abfdb433e5215c8d981d0df86"
  }
  </code></pre>
  
  <p>relative records will have the same <code>threadId</code>, and I want to to group them together with <code>threadId</code> in aggregation query like this:</p>
  
  <pre><code>{
    _id: e9f800dc3c6dca0b98604bc08ad5ffec17c2aba1 //&lt;- this is the `threadId`
    record:[
         {
              "_id" : ObjectId("558cd1c4bf46acde47a0d4f6"),
              "content" : "That is very good",
              "createTime" : NumberLong("1435292100235"),
              "deleted" : false,
              "parent" : "",
              "poiId" : "538318500cf2e33469a2d819",
              "poiType" : "event",
              "role" : "user",
              "threadId" : "e9f800dc3c6dca0b98604bc08ad5ffec17c2aba1",
              "thumbsUp" : 0,
              "userId" : "53a85640f66cce8abfdb433e5215c8d981d0df86"
          }
          {
              "_id" : ObjectId("558cd375bf46acde47a0d4f7"),
              "content" : "That is very good",
              "createTime" : NumberLong("1435292533764"),
              "deleted" : false,
              "parent" : "558cd1c4bf46acde47a0d4f6",
              "poiId" : "538318500cf2e33469a2d819",
              "poiType" : "event",
              "role" : "user",
              "threadId" : "e9f800dc3c6dca0b98604bc08ad5ffec17c2aba1",
              "thumbsUp" : 0,
              "userId" : "53a85640f66cce8abfdb433e5215c8d981d0df86"
          }
  
    ],
   count: 2
  },
  {
    _id:cefd5af2429b42b3b97295db53369af6f1e033c8
    record:[
         {
              "_id" : ObjectId("558cd1c4bf46acde47a0d4f6"),
              "content" : "That is very good",
              "createTime" : NumberLong("1435292100235"),
              "deleted" : false,
              "parent" : "",
              "poiId" : "538318500cf2e33469a2d819",
              "poiType" : "event",
              "role" : "user",
              "threadId" : "e9f800dc3c6dca0b98604bc08ad5ffec17c2aba1",
              "thumbsUp" : 0,
              "userId" : "53a85640f66cce8abfdb433e5215c8d981d0df86"
          }
  
    ],
    count: 1
  
  }
  ....
  </code></pre>
  
  <p>So far I can write a aggregate query:</p>
  
  <pre><code>db.comment.aggregate([
      {"$match":{"poiId":"538318500cf2e33469a2d819"}},
      {"$group":{
          "_id":"$threadId",
          record:{$first:"$$ROOT"}
          }
      }
   ])
  </code></pre>
  
  <p>I'm not very familiar with mongodb's aggregation mechanism it seems <code>$first</code> is not right, How can I do this? </p>