MongoDB聚合使用distinct

时间:2013-07-25 01:28:09

标签: mongodb

我有一个聚合,在日期上分组并创建一个总和。

db.InboundWorkItems.aggregate({
    $match: {
        notificationDate: {
            $gte: ISODate("2013-07-18T04:00:00Z")
        },
        dropType: 'drop'
    }
}, {
    $group: {
        _id: {
            notificationDate: "$notificationDate"
        },
        nd: {
            $first: "$notificationDate"
        },
        count: {
            $sum: 1
        }
    }
}, {
    $sort: {
        nd: 1
    }
})

输出

"result" : [
    {
        "_id" : {
            "notificationDate" : ISODate("2013-07-18T04:00:00Z")
        },
        "nd" : ISODate("2013-07-18T04:00:00Z"),
        "count" : 484
    },
    {
        "_id" : {
            "notificationDate" : ISODate("2013-07-19T04:00:00Z")
        },
        "nd" : ISODate("2013-07-19T04:00:00Z"),
        "count" : 490
    },
    {
        "_id" : {
            "notificationDate" : ISODate("2013-07-20T04:00:00Z")
        },
        "nd" : ISODate("2013-07-20T04:00:00Z"),
        "count" : 174
    },
    {
        "_id" : {
            "notificationDate" : ISODate("2013-07-21T04:00:00Z")
        },
        "nd" : ISODate("2013-07-21T04:00:00Z"),
        "count" : 6
    },
    {
        "_id" : {
            "notificationDate" : ISODate("2013-07-22T04:00:00Z")
        },
        "nd" : ISODate("2013-07-22T04:00:00Z"),
        "count" : 339
    },
    {
        "_id" : {
            "notificationDate" : ISODate("2013-07-23T04:00:00Z")
        },
        "nd" : ISODate("2013-07-23T04:00:00Z"),
        "count" : 394
    },
    {
        "_id" : {
            "notificationDate" : ISODate("2013-07-24T04:00:00Z")
        },
        "nd" : ISODate("2013-07-24T04:00:00Z"),
        "count" : 17
    }
],
"ok" : 1
到目前为止一切顺利。我现在需要做的是保持这个,但也在标准中添加一个独特的(为了参数的缘故,我想使用AccountId)。只使用不同的AccountId,我将获得分组日期的计数。在聚合框架中甚至是不可能的?

3 个答案:

答案 0 :(得分:0)

您可以在管道中使用两个组命令,第一个按accoundId分组,然后是第二个执行常规操作的组。像这样的东西:

db.InboundWorkItems.aggregate( 
  {$match: {notificationDate: {$gte: ISODate("2013-07-18T04:00:00Z")}, dropType:'drop' }}, 
  {$group: {_id:"accountId",notificationDate:"$notificationDate"}},
  {$group: {_id:1, nd: {$first:"$notificationDate"}, count:{$sum:1} }}, 
  {$sort:{nd:1}}  )

答案 1 :(得分:0)

db.InboundWorkItems.aggregate({
    $match: {
        notificationDate: {
            $gte: ISODate("2013-07-18T04:00:00Z")
        },
        dropType: 'drop'
    }
}, {
    $group: {
        _id: "$AccountId",
        notificationDate: {
            $max: "$notificationDate"
        },
        dropType: {
            $max: "$dropType"
        }
    }

}, {
    $group: {
        _id: {
            notificationDate: "$notificationDate"
        },
        nd: {
            $first: "$notificationDate"
        },
        count: {
            $sum: 1
        }
    }
}, {
    $sort: {
        nd: 1
    }
})

答案 2 :(得分:0)

我认为你可能实际上正在寻找一个单一的群体(英语有点令人困惑),如下:

db.InboundWorkItems.aggregate({
    $match: {
        notificationDate: {
            $gte: ISODate("2013-07-18T04:00:00Z")
        },
        dropType: 'drop'
    }
}, {
    $group: {
        _id: {
            notificationDate: "$notificationDate", accountId: '$accountId'
        },
        nd: {
            $first: "$notificationDate"
        },
        count: {
            $sum: 1
        }
    }
}, {
    $sort: {
        nd: 1
    }
})

我在$ group中添加了复合_id,因为:

  

只会使用不同的AccountId来计算分组日期。

这让我觉得您希望按帐户ID分组日期计数。

相关问题