0

i am getting data using nodejs with MongoDB. I have objects in the MongoDB. and now I am going for the getting data using date wise and my datetime filed is a timestamp. and I want to get data from start date to end date using MongoDB. must imp note i want to print date in the my expected op. here I this is my objects =>

{
  "_id" : ObjectId("595be16ee04602135828e25c"),
  "Action" : "Comment",
  "datetime" : 1507099928000 // 4th oct 2017 convert date just for info here write
},
{
  "_id" : ObjectId("595be16ee04602135828e25c"),
  "Action" : "Comment",
   "datetime" : 1508139441716 // 16th oct 2017 convert date just for info here write
}
{
   "_id" : ObjectId("595be16ee04602135828e25c"),
   "Action" : "Comment",
   "datetime" : 1508139441716 // 16th oct 2017 convert date just for info here write
}
{
  "_id" : ObjectId("595be16ee04602135828e25c"),
  "Action" : "Like",
   "datetime" : 1508139441716 // 16th oct 2017 convert date just for info here write
 },

this is my query =>

InstaAc.aggregate([
                    {
                        "$match": {
                            "_id": ObjectId("595be16ee04602135828e25c"),
                            "datetime": {
                           "$lte": 1508141028150, "$gte": 1507622568000
                            }
                        },
                            "Action": {
                                $in: ["Comment", "Like"]
                            }
                    },                                              
                    {
                        "$addFields": {
                            "datetime": {
                                "$add": [new Date(0), "$datetime"]
                            }
                        }
                    },
                        {
                            "$group": {
                                "_id": {
                                    "$dateToString": {
                                        "format": "%d-%m-%Y",
                                        "date": "datetime"
                                    }
                                },
                               "commentcount": { $sum: { $cond: [{ $eq: ["Action", "Comment"] }, 1, 0] } },
                                "likecount": { $sum: { $cond: [{ $eq: ["Action", "Like"] }, 1, 0] } },                                  
                            }
                        },                           
                        {
                            "$sort": {
                                "_id": 1
                            }
                        }
           ]).exec(function (err, data) {
            if (err) {
console.log(err); 
}
else {
console.log(data);        
}
 })

this is my query above and I am getting an error like this "date is not defined" please, anyone, know how can fix this then please help me.

my excepted o/p =>

{ _id: '16-10-2017', commentcount: 2 ,likecount:1},
Edit
  • 385
  • 4
  • 24

1 Answers1

0

Before you run your query create a date object and use that object instead of new Date(0).

var myDate = new Date(0);

And in your query

"$addFields": {
               "datetime": {
               "$add": [myDate, "$datetime"]
              }
H. Hakvoort
  • 161
  • 1
  • 2
  • 14