1

I want to go show data in the chart using date wise last 7 days date with data.and i am using this for database is mongodb and working in nodejs. i have create also one query for that generate date wise count but i am not getting success. i am getting count whole date wise. not apply group by on date. and in the database i have store datetime in timestamp so i need to here work with only date.here i have also generate last 7 day and generate lastdate in the timestamp and apply one condition lastdate greater than or equal date to get count. so here i have listed my code and object so any see and have the idea how can fix it then please let me know.

This is my array of 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
}

my current o/p=>

{ _id: null, count: 8 }

my expecated 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" }

here this is my query =>

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]);                                   
                            }
                        })
Edit
  • 385
  • 4
  • 24
  • Possible duplicate of [how to get count date wise using group by from the array object in mongodb?](https://stackoverflow.com/questions/46617530/how-to-get-count-date-wise-using-group-by-from-the-array-object-in-mongodb) – s7vr Oct 10 '17 at 12:43

1 Answers1

-1

in query you have to modify this line

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

in here you have to group dateandtime and also action wise

and in array of object you have to convert your timestamp into new date

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

you get output

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

in projection you could include like

{
    "$project": {
        _id:0,
        "yearMonthDay": { 
            $dateToString: { 
                format: "%d-%m-%Y", 
                date: "$_id.datetime" 
            } 
        },
        action:"$_id.action",
        count:1 
    }
}
Florian Koch
  • 1,372
  • 1
  • 30
  • 49
Anushka Ahir
  • 136
  • 7