Date MongoDB的记录数

Sye*_*ain 5 mongodb

这是一个记录客户收藏

 {
    name: xyz,
    .
    .
    .
    createdAt: Sun Nov 20 2016 00:00:00 GMT+0530 (IST)
    lastModified: Sat Dec 10 2016 00:00:00 GMT+0530 (IST)
 }
Run Code Online (Sandbox Code Playgroud)

我需要能够根据日期对客户进行修改

eg: Date                Count(lastModified)
    11-20-2016           10
    12-20-2016            7
    13-20-2016            9
Run Code Online (Sandbox Code Playgroud)

我希望结果分组这样的东西.

Sha*_*Roy 11

如果你想按lastModified 日期和时间计算,那么可以使用如下:

db.getCollection('collectionName').aggregate({$group:{_id: "$lastModified", count:{$sum:1}}})
Run Code Online (Sandbox Code Playgroud)

如果你想按lastModified 日期计算,那么可以使用如下:

db.getCollection('collectionName').aggregate(
[
    {
        $group:
        {
            _id:
            {
                day: { $dayOfMonth: "$lastModified" },
                month: { $month: "$lastModified" }, 
                year: { $year: "$lastModified" }
            }, 
            count: { $sum:1 },
            date: { $first: "$lastModified" }
        }
    },
    {
        $project:
        {
            date:
            {
                $dateToString: { format: "%Y-%m-%d", date: "$date" }
            },
            count: 1,
            _id: 0
        }
    }
])
Run Code Online (Sandbox Code Playgroud)