如何在mongodb聚合中对具有特定字段的文档进行分组 [英] How to group documents with specific field in aggregation of mongodb

查看:31
本文介绍了如何在mongodb聚合中对具有特定字段的文档进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 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"
}

相关记录将具有相同的 threadId,我想在聚合查询中将它们与 threadId 组合在一起,如下所示:

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"}
        }
    }
 ])

我对mongodb的聚合机制不是很熟悉,看来$first不对,我该怎么做?

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

推荐答案

$$ROOT - 引用根文档,表示当前正在聚合管道阶段处理的顶级文档.

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

您应该使用以下查询:

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

如果要添加 match 条件,请使用以下查询 -

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()

这篇关于如何在mongodb聚合中对具有特定字段的文档进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆