我在mongodb中有一组文档,如下所示:
{
"_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"
}
相对记录将具有相同的threadId
,我想在聚合查询中将它们与threadId
组合在一起,如下所示:
{
_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
}
....
到目前为止,我可以编写一个聚合查询:
db.comment.aggregate([
{"$match":{"poiId":"538318500cf2e33469a2d819"}},
{"$group":{
"_id":"$threadId",
record:{$first:"$$ROOT"}
}
}
])
我不太熟悉mongodb的聚合机制——$first
似乎不对,我该怎么做?
$$ROOT-引用根文档,表示当前正在聚合管道阶段处理的顶级文档。
您应该使用以下查询:
db.collection.aggregate({
$group: {
_id: "$threadId",
"records": {
$push: "$$ROOT"
},
"count": {
$sum: 1
}
}
}).pretty()
如果您想添加match
标准,请使用以下查询-
db.collection.aggregate({
$match: {
"poiId": "538318500cf2e33469a2d819" //can change match parameters
}
}, {
$group: {
_id: "$threadId",
"records": {
$push: "$$ROOT"
},
"count": {
$sum: 1
}
}
}).pretty()