0

here i have create one query for the show count date wise in the chart. and i have used mongodb. and i have one object and in the object store multiple data and separate with one value Action. i want to show count date wise Action like Comment,like both action count want show. right now i am just show comment count. i want also get like count. here below i have listed my object and query.

here this is my array of the 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: '03-10-2017', count: 1 },
  { _id: '04-10-2017', count: 2 },
  { _id: '05-10-2017', count: 2 },
  { _id: '06-10-2017', count: 2 } ]

my excepted o/p =>

[ { _id: '03-10-2017', count: 1 ,likecount:0},
  { _id: '04-10-2017', count: 2 ,likecount:0},
  { _id: '05-10-2017', count: 2,likecount:0 },
  { _id: '06-10-2017', count: 2,liekcount:1},     

this is my query =>

InstaAc.aggregate([
                        {
                            "$match": {
                                "_id": ObjectId("595e3b2033961713940442cf"),
                                "History.datetime": {
                               "$lte": 1507529239000, "$gte": 1507012285558
                                }
                            }
                        },
                        {
                            "$unwind": "$History"
                        },
                        {
                            "$match": {
                                "History.datetime": {
                                  "$lte": 1507529239000, "$gte": 1507012285558
                                },
                                "History.Action": {
                                    $eq: "comment"
                                }
                            }
                        },
                        {
                            "$addFields": {
                                "History.datetime": {
                                    "$add": [new Date(0), "$History.datetime"]
                                }
                            }
                        },
                            {
                                "$group": {
                                    "_id": {
                                        "$dateToString": {
                                            "format": "%d-%m-%Y",
                                            "date": "$History.datetime"
                                        }
                                    },
                                    "count": {
                                        "$sum": 1
                                    }                                    
                                }
                            },                           
                            {
                                "$sort": {
                                    "_id": 1
                                }
                            }
               ]).exec(function (err, data) {
if (err) {
    console.log(err); 
}
else {
    console.log(data);        
}
 })

any one how can do that then please let me know.

Edit
  • 385
  • 4
  • 24
  • Something like `{ "$match": { "_id": ObjectId("595e3b2033961713940442cf"), "History.datetime": { "$lte": 1507529239000, "$gte": 1507012285558 }, History.Action: {$in:["Comment", "like"]}}}` for both match stages – s7vr Oct 11 '17 at 08:57
  • yes i am try but return same o/p – Edit Oct 11 '17 at 08:58
  • how can add onther new count in the o/p for like? – Edit Oct 11 '17 at 08:58
  • and i have tow $match statement show in which statement in to add $unwind above or below ? – Edit Oct 11 '17 at 09:00
  • Please read the comment carefully. You should include it both matches stage. – s7vr Oct 11 '17 at 09:01
  • yes i do but this like count add in to comment count. not show onther ,likecount = 1 – Edit Oct 11 '17 at 09:03
  • i want like this { _id: '06-10-2017', count: 2,liekcount:1} – Edit Oct 11 '17 at 09:05
  • To get expected output you have to update $group to include likecount field. `likecount:{$sum:{$cond:[{$eq:["$History.Action","like"]}, 1, 0]}}` – s7vr Oct 11 '17 at 09:07
  • yes thank you so much finally i get it result again thank you so muc – Edit Oct 11 '17 at 09:11

1 Answers1

1

Update first $match to

{
  "$match": {
    "_id": ObjectId("595e3b2033961713940442cf"),
    "History.datetime": {
      "$lte": 1507529239000,
      "$gte": 1507012285558
    },
    "History.Action": {
      "$in": [
        "Comment",
        "like"
      ]
    }
  }
}

and second $match to

{
  "$match": {
    "History.datetime": {
      "$lte": 1507529239000,
      "$gte": 1507012285558
    },
    "History.Action": {
      "$in": [
        "Comment",
        "like"
      ]
    }
  }
}

and $group to

{
  "likecount": {
    "$sum": {
      "$cond": [
        {
          "$eq": [
            "$History.Action",
            "like"
          ]
        },
        1,
        0
      ]
    }
  }
}
s7vr
  • 73,656
  • 11
  • 106
  • 127
  • Hello i am working with now object and i am make new query for the object then after i am getting error "date is not defind" so how can fix it? – Edit Oct 16 '17 at 08:56
  • using this same for o/p – Edit Oct 16 '17 at 09:02
  • https://stackoverflow.com/questions/46766368/getting-error-like-date-is-not-defind-using-nodejs-with-mogodb please visit this – Edit Oct 16 '17 at 09:19
  • how can i print my date again i am getting error. actully i have changed my schema so right now i am getting count form the object so now i am getting error for the date and don't print date. – Edit Oct 16 '17 at 10:06
  • Hi please help on this post and give me some hint https://stackoverflow.com/questions/47072791/how-to-remove-time-form-unixtime-stamp-date-using-mongodb?noredirect=1#comment81093717_47072791 – Edit Nov 02 '17 at 12:05
  • i am getting some issue on generate dates with remove time form the date. – Edit Nov 02 '17 at 12:06