Date MongoDB的记录数

时间:2016-12-15 06:01:03

标签: mongodb

这是一个记录客户收藏

    .support-box {
        width: 50%;
        float: left;
        display: block;
        height: 20rem; /* is support box height you can change as per your requirement*/
        background-color:#000;
    }
    .wrapper {
        width: 50%;
        display: block;
        position: relative;
        top: 50%;
        transform: translateY(-50%);
         background:#ddd;
       margin:auto;
       height:100px; /* here the height values are automatic you can leave this if you can*/

    }
    .text {
        width: 100%;
        display: block;
        padding:10px;
        margin:auto;
    }


    <div class="support-box">
    <div class="wrapper">
    <div class="text">USE OUR DESIGNS</div>
    </div>
    </div>

Js fiddle:// https://jsfiddle.net/vh4y426f/5/

我需要能够根据日期计算客户数量

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

我希望结果分组如下。

4 个答案:

答案 0 :(得分:9)

如果您想按lastModified 日期和时间计算,则可以使用如下:

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

如果您想按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
        }
    }
])

答案 1 :(得分:1)

这更简单:

db.getCollection('collectionName').aggregate(
[
    {
        $group:
        {
            _id: {
                $dateToString: {
                    "date": "$lastModified",
                    "format": "%Y-%m-%d"
                }
            }, 
            count: { $sum:1 }
        }
    }
])

答案 2 :(得分:0)

我把这个按yii2排序

$aggregateResult = Yii::$app->mongodb->getCollection('patient')->aggregate([
            [
                '$match' => [
                    '$and' => [
                        ['deleted_at' => ['$exists' =>  false]],
                        ['registration_date' => ['$gte' =>  $startDateStamp, '$lte' => $endDateStamp]],
                    ]
                ]
            ],
            [
                '$group' => [
                    '_id' =>  [
                        'day' => ['$dayOfMonth' =>  ['$toDate' => ['$multiply' => ['$registration_date', 1000]]]],
                        'month' => ['$month' => ['$toDate' => ['$multiply' => ['$registration_date', 1000]]]],
                        'year' => ['$year' => ['$toDate' => ['$multiply' => ['$registration_date', 1000]]]]
                    ],
                    'count' => ['$sum' => 1],
                    'date' => ['$first' => ['$toDate' => ['$multiply' => ['$registration_date', 1000]]]]
                ]
            ],

            [
                '$project' => [
                    'date' => ['$dateToString' => ['format' => '%Y-%m-%d', 'date' => '$date']],
                    'count' => 1,
                    '_id' => 0
                ]
            ],
            ['$sort' => ['date' => 1]],

        ]);

答案 3 :(得分:0)

我按以下方式计算了我的日期

示例数据:

{
    "_id" : ObjectId("5f703ef9db38661df02d3a77"),
    "color" : "black",
    "icon" : "grav",
    "name" : "viewed count",
    "created_date" : ISODate("2020-09-27T07:27:53.816Z"),
    "updated_date" : ISODate("2021-05-11T19:11:14.415Z"),
    "read" : false,
    "graph" : [ 
        {
            "_id" : ObjectId("5f704286db38661df02d3a81"),
            "count" : 1,
            "date" : ISODate("2020-09-27T07:43:02.231Z")
        }, 
        {
            "_id" : ObjectId("5f7043ebdb38661df02d3a88"),
            "date" : ISODate("2020-09-27T07:48:59.383Z"),
            "count" : 2
        }]
}

我的查询是:

db.test.aggregate(
  [{$match:{"name" : "viewed count"}},
   {$unwind:'$graph'},
    {
      $match: {
        'graph.date': {
          $gt: ISODate("2018-09-27"),
          $lt: ISODate("2021-09-27")
        }
      }
    },
    {
      $project: {
        date: {$dateToString: {format: '%Y-%m-%d', date: '$graph.date'}}
      },
    },
    {
      $group: {
        _id: "$date",
        count: {$sum: 1}
      }
    },
    {
      $sort: {
        _id: 1
      }
    }
  ]
)

输出:

[{
    "_id" : "2020-09-27",
    "count" : 58.0
},
{
    "_id" : "2020-09-29",
    "count" : 50.0
}]