按日期分组并使用mongodb明智地计算日期?

时间:2017-10-10 08:51:44

标签: mongodb

我想在数据中使用日期明确的最后7天日期显示图表中的数据。我使用这个数据库是mongodb并在nodejs中工作。我也创建了一个查询,生成日期明智的计数,但我没有成功。我明智地计算整个约会。不按日期申请分组。在数据库中我有时间戳存储日期时间所以我需要在这里只使用date.here我还生成最后7天并在时间戳中生成lastdate并应用一个条件lastdate大于或等于日期来获取计数。所以在这里我列出了我的代码和对象,所以任何看到并有想法如何解决它然后请让我知道。

这是我的object =>

数组
{
  "_id" : ObjectId("578fa05a7391bb0d34bd3c28"),
  "Action" : "Comment",
  "datetime" : 1507099928000 // 4th oct 2017 convert date just for info here write
},
{
   "_id" : ObjectId("578fa05a7391bb0d34bd3c28"),
    "Action" : "Comment",
    "datetime" : 1507099928000  // 4th oct 2017 convert date just for info here write
}
{
    "_id" : ObjectId("578fa05a7391bb0d34bd3c30"),
     "Action" : "Comment",
     "datetime" : 1507186328000 // 5th oct 2017 convert date just for info here write
}
{
  "_id" : ObjectId("578fa05a7391bb0d34bd3c28"),
  "Action" : "Comment",
  "datetime" : 1507193528000 // 5th oct 2017 convert date just for info here write
},
{
   "_id" : ObjectId("578fa05a7391bb0d34bd3c28"),
    "Action" : "Comment",
    "datetime" : 1507279928000 // 6th oct 2017 convert date just for info here write
}
{
    "_id" : ObjectId("578fa05a7391bb0d34bd3c30"),
     "Action" : "Comment",
     "datetime" : 1507020728000 // 3th oct 2017 convert date just for info here write
}
{
  "_id" : ObjectId("578fa05a7391bb0d34bd3c28"),
  "Action" : "Comment",
  "datetime" : 1507279928000  // 6th oct 2017 convert date just for info here write
},
 {
  "_id" : ObjectId("578fa05a7391bb0d34bd3c28"),
  "Action" : "like",
  "datetime" : 1507279928000  // 6th oct 2017 convert date just for info here write
}

我当前的o / p =>

{ _id: null, count: 8 }

我的预期o / p =>

{ _id: 3-10-2017, count: 1,Action = "comment" }
{ _id: 4-10-2017, count: 2,Action = "comment" }
{ _id: 5-10-2017, count: 2,Action = "comment" }
{ _id: 6-10-2017, count: 2,Action = "comment" }

这是我的查询=>

var lastdate = 1507012285558; // this is last date 3rd oct 2017 now here to greate than date find and get count

   InstaAc.aggregate([
                        {
                            "$match": {
                                "_id": ObjectId("595e3b2033961713940442cf"),
                                "History.datetime": {
                                    "$gte": lastdate
                                }
                            }
                        },
                        {
                            "$unwind": "$History"
                        },
                        {
                            "$match": {
                                "History.datetime": {
                                    "$gte": lastdate
                                },
                                "History.Action": {
                                    $eq: "comment"
                                }
                            }
                        },                  
                        {
                            "$group": {
                                "_id": "$_id",       
                                "count": {
                                    "$sum": 1
                                },
                                "History": {
                                    "$push": "$History"
                                }
                            }
                        },
                        {
                            "$sort": {
                                "_id": 1
                            }
                        }
                        ]).exec(function (err, data) {
                            if (err) {
                                console.log(err); 
                            }
                            else {                                  
                                console.log(data[0]);                                   
                            }
                        })

1 个答案:

答案 0 :(得分:-1)

在查询中你必须修改这一行

{
    "$group": {
        "_id": {datetime:"$datetime",action:"$Action"},
        "count": {
            "$sum": 1
        },
    }
}

在这里你必须将日期和时间分组并且还要明智地行动

并且在对象数组中,您必须将时间戳转换为新日期

{
  "_id" : ObjectId("578fa05a7391bb0d34bd3c28"),
  "Action" : "Comment",
  "datetime" :new Date(1507099928000)
},

你得到输出

{
    "_id" : ObjectId("578fa05a7391bb0d34bd3c28"),
    "Action" : "Comment",
    "datetime" : ISODate("2017-10-04T06:52:08.000Z")
}

在投影中你可以包括像

{
    "$project": {
        _id:0,
        "yearMonthDay": { 
            $dateToString: { 
                format: "%d-%m-%Y", 
                date: "$_id.datetime" 
            } 
        },
        action:"$_id.action",
        count:1 
    }
}