查找基于groupby,count和max createDate的所有记录

时间:2015-05-22 18:52:47

标签: mongodb aggregation-framework

我想在同一个文档中找到基于groupby,count和createDate最大的详细信息的所有记录,以下是一个示例集合:

/* 0 */
{
    "_id" : 1,
    "name" : "pradeep",
    "age" : 26,
    "createDate" : ISODate("2015-06-20T22:14:41.423-18:30")
}

/* 1 */
{
    "_id" : 2,
    "name" : "pradeep",
    "age" : 26,
    "createDate" : ISODate("2015-05-21T22:14:50.598-18:30")
}

/* 2 */
{
    "_id" : 3,
    "name" : "pradeep",
    "age" : 26,
    "createDate" : ISODate("2015-05-21T22:14:54.250-18:30")
}

/* 3 */
{
    "_id" : 4,
    "name" : "pradeep",
    "age" : 24,
    "createDate" : ISODate("2015-04-21T22:15:21.720-18:30")
}

/* 4 */
{
    "_id" : 5,
    "name" : "pradeep",
    "age" : 21,
    "createDate" : ISODate("2015-12-21T22:16:31.676-18:30")
}

/* 5 */
{
    "_id" : 6,
    "name" : "pradeep",
    "age" : 24,
    "createDate" : ISODate("2015-05-21T22:16:31.678-18:30")
}

/* 6 */
{
    "_id" : 7,
    "name" : "pradeep",
    "age" : 24,
    "createDate" : ISODate("2015-05-21T22:16:31.678-18:30")
}

/* 7 */
{
    "_id" : 8,
    "name" : "pradeep",
    "age" : 24,
    "createDate" : ISODate("2015-02-21T22:16:31.678-18:30")
}

/* 8 */
{
    "_id" : 9,
    "name" : "pradeep",
    "age" : 24,
    "createDate" : ISODate("2015-11-21T22:16:31.679-18:30")
}

/* 9 */
{
    "_id" : 10,
    "name" : "pradeep",
    "age" : 23,
    "createDate" : ISODate("2015-05-21T22:16:31.679-18:30")
}

/* 10 */
{
    "_id" : 11,
    "name" : "pradeep gupta",
    "age" : 27,
    "createDate" : ISODate("2015-05-21T22:16:31.679-18:30")
}

/* 11 */
{
    "_id" : 12,
    "name" : "ram",
    "age" : 21,
    "createDate" : ISODate("2015-08-21T22:17:21.014-18:30")
}

/* 12 */
{
    "_id" : 13,
    "name" : "ram",
    "age" : 27,
    "createDate" : ISODate("2015-05-21T22:17:21.015-18:30")
}

/* 13 */
{
    "_id" : 14,
    "name" : "ram",
    "age" : 28,
    "createDate" : ISODate("2015-05-21T22:17:21.016-18:30")
}

/* 14 */
{
    "_id" : 16,
    "name" : "lucky",
    "age" : 27,
    "createDate" : ISODate("2015-09-21T22:18:09.985-18:30")
}

/* 15 */
{
    "_id" : 17,
    "name" : "zawahir",
    "age" : 28,
    "createDate" : ISODate("2015-05-21T22:18:09.986-18:30")
}

/* 16 */
{
    "_id" : 15,
    "name" : "lucky",
    "age" : 21,
    "createDate" : ISODate("2015-05-21T22:18:23.134-18:30")
}

如何使用groupby名称找到createDate最大的所有记录,我还想要使用mongoDB Query分组的总数? 想要这样的输出:

[{
        "_id" : "lucky",
        "countTotal" : 2,
        "MaxCreateDate" : ISODate("2015-09-22T22:18:09.985+05:30"),
        "MaxCreateDateUserDetails" :{_id: "lucky", name:"",age:""}
    },{
           ......
}] 

1 个答案:

答案 0 :(得分:1)

使用 aggregation framework 来获得所需的结果。以下聚合管道完成了这项工作。它使用$ group运算符按name标识符表达式对输入文档进行分组,并将累加器表达式$max$sum$first应用于每个组:

- 编辑 -

获取MaxCreateDateUserDetails您需要在系统变量Group Accumulator Operator上应用 $first $$ROOT,该变量返回对根的引用文档,即顶级文档,当前正在汇总管道阶段进行处理。

db.collection.aggregate([
    {
        "$group": {
            "_id": "$name",
            "MaxCreateDate": {
                "$max": "$createDate"
            },
            "count": {
                "$sum": 1
            },
            "MaxCreateDateUserDetails": {
                "$first": "$$ROOT"
            }               
        }
    },
    {
        "$project": {
            "MaxCreateDate": 1,
            "count": 1,
            "MaxCreateDateUserDetails": {
                "_id": "$_id",
                "name": "$MaxCreateDateUserDetails.name",
                "age" : "$MaxCreateDateUserDetails.age",
            }               
        }
    }
])

<强>结果

/* 1 */
{
    "result" : [ 
        {
            "_id" : "zawahir",
            "MaxCreateDate" : ISODate("2015-05-22T16:48:09.986Z"),
            "count" : 1,
            "MaxCreateDateUserDetails" : {
                "_id" : "zawahir",
                "name" : "zawahir",
                "age" : 28
            }
        }, 
        {
            "_id" : "lucky",
            "MaxCreateDate" : ISODate("2015-09-22T16:48:09.985Z"),
            "count" : 2,
            "MaxCreateDateUserDetails" : {
                "_id" : "lucky",
                "name" : "lucky",
                "age" : 27
            }
        }, 
        {
            "_id" : "ram",
            "MaxCreateDate" : ISODate("2015-08-22T16:47:21.014Z"),
            "count" : 3,
            "MaxCreateDateUserDetails" : {
                "_id" : "ram",
                "name" : "ram",
                "age" : 21
            }
        }, 
        {
            "_id" : "pradeep gupta",
            "MaxCreateDate" : ISODate("2015-05-22T16:46:31.679Z"),
            "count" : 1,
            "MaxCreateDateUserDetails" : {
                "_id" : "pradeep gupta",
                "name" : "pradeep gupta",
                "age" : 27
            }
        }, 
        {
            "_id" : "pradeep",
            "MaxCreateDate" : ISODate("2015-12-22T16:46:31.676Z"),
            "count" : 10,
            "MaxCreateDateUserDetails" : {
                "_id" : "pradeep",
                "name" : "pradeep",
                "age" : 26
            }
        }
    ],
    "ok" : 1
}