Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to group documents with specific field in aggregation of mongodb

I have a collection of documents in mongodb like this:

{
    "_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"
}

relative records will have the same threadId, and I want to to group them together with threadId in aggregation query like this:

{
  _id: e9f800dc3c6dca0b98604bc08ad5ffec17c2aba1 //<- 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

}
....

So far I can write a aggregate query:

db.comment.aggregate([
    {"$match":{"poiId":"538318500cf2e33469a2d819"}},
    {"$group":{
        "_id":"$threadId",
        record:{$first:"$$ROOT"}
        }
    }
 ])

I'm not very familiar with mongodb's aggregation mechanism it seems $first is not right, How can I do this?

like image 677
armnotstrong Avatar asked Jun 26 '15 05:06

armnotstrong


1 Answers

$$ROOT - References the root document that means the top-level document which is currently being processed in the aggregation pipeline stage.

You should use following query :

db.collection.aggregate({
    $group: {
    _id: "$threadId",
    "records": {
        $push: "$$ROOT"
    },
    "count": {
        $sum: 1
    }
    }
}).pretty()

If you want to add match criteria then use following query -

db.collection.aggregate({
    $match: {
    "poiId": "538318500cf2e33469a2d819" //can change match parameters
    }
}, {
    $group: {
    _id: "$threadId",
    "records": {
        $push: "$$ROOT"
    },
    "count": {
        $sum: 1
    }
    }
}).pretty()
like image 105
Vishwas Avatar answered Oct 10 '22 03:10

Vishwas