日期数据使用mongodb错误?

时间:2017-11-02 06:11:12

标签: mongodb

您好我将使用monogdb从2个日期开始到最后一个日期之间获取数据。我获得成功获得2个日期之间的数据,但这里有一些问题,获取数据,我将去除时间,然后只比较日期,然后当前日期日数据不来这里所以任何想法我的错误在哪里请告诉我。这是我的代码。

这是我的文件=>

        {
         "_id" : ObjectId("578fa05a7391bb0d34bd3c28"),
          "InId": ObjectId("595e3b2033961713940442cd")
          "Action" : "Comment",
          "datetime" : 1509084155000 // 27th oct 2017 convert date just for info here write
     },
     {
           "_id" : ObjectId("578fa05a7391bb0d34bd3c28"),
          "InId": ObjectId("595e3b2033961713940442cd")
           "Action" : "Comment",
           "datetime" : 1509084155000  // 27th oct 2017 convert date just for info here write
      },
      {
          "_id" : ObjectId("578fa05a7391bb0d34bd3c30"),
          "InId": ObjectId("595e3b2033961713940442cd")
          "Action" : "Comment",
          "datetime" : 1509084155000 // 27th oct 2017 convert date just for info here write
      },
      {
         "_id" : ObjectId("578fa05a7391bb0d34bd3c28"),
          "InId": ObjectId("595e3b2033961713940442cd")
         "Action" : "Comment",
         "datetime" : 1509170555000 // 28th oct 2017 convert date just for info here write
    },
    {
           "_id" : ObjectId("578fa05a7391bb0d34bd3c28"),
          "InId": ObjectId("595e3b2033961713940442cd")
           "Action" : "Comment",
           "datetime" : 1509170555000 // 28th oct 2017 convert date just for info here write
    },
    {
            "_id" : ObjectId("578fa05a7391bb0d34bd3c30"),
          "InId": ObjectId("595e3b2033961713940442cd")
            "Action" : "Comment",
            "datetime" : 1509343355000 // 30th oct 2017 convert date just for info here write
    }
    {
           "_id" : ObjectId("578fa05a7391bb0d34bd3c28"),
          "InId": ObjectId("595e3b2033961713940442cd")
           "Action" : "Comment",
           "datetime" : 1509343355000  // 30th oct 2017 convert date just for info here write
     },
     {
            "_id" : ObjectId("578fa05a7391bb0d34bd3c28"),
          "InId": ObjectId("595e3b2033961713940442cd")
            "Action" : "like",
            "datetime" : 1509429755000  // 31th oct 2017 convert date just for info here write
    },
     {
            "_id" : ObjectId("578fa05a7391bb0d34bd3c28"),
          "InId": ObjectId("595e3b2033961713940442cd")
            "Action" : "like",
            "datetime" : 1509516155000 // 1th Nov 2017 convert date just for info here write
     },
     {
            "_id" : ObjectId("578fa05a7391bb0d34bd3c28"),
          "InId": ObjectId("595e3b2033961713940442cd")
            "Action" : "like",
            "datetime" : 1509611470000// 2th Nov 2017 convert date just for info here write
     }

我当前的o / p =>

  { _id: '27-10-2017', CommentCount: 3 ,Likecount:0},
  { _id: '28-10-2017', CommentCount: 2 ,Likecount:0},
  { _id: '30-10-2017', CommentCount: 2,Likecount:0 },
  { _id: '31-10-2017', CommentCount: 0,Likecount:1 },
  { _id: '01-11-2017', CommentCount: 0 ,Likecount:1},

我的例外情况o / p =>

  { _id: '27-10-2017', CommentCount: 3 ,Likecount:0},
  { _id: '28-10-2017', CommentCount: 2 ,Likecount:0},
  { _id: '30-10-2017', CommentCount: 2,Likecount:0 },
  { _id: '31-10-2017', CommentCount: 0,Likecount:1 },
  { _id: '01-11-2017', CommentCount: 0 ,Likecount:1},
  { _id: '02-11-2017', CommentCount: 0 ,Likecount:1},

这是我的查询=>

     function removetime(unx) {
        var ld = new Date(parseInt(unx))
        return ld.setUTCHours(0, 0, 0, 0);
       }
           ActivityHistory.aggregate([
              {
                  "$match": {
                      "InId": ObjectId("595e3b2033961713940442cd"),
                      "datetime": {
                          "$lte": removetime("1509602915994"), "$gte": removetime("1509084515994") // removetime funcation used for remove time 
                      }
                  }
              },
                {
                    "$project": {
                        datetime: 1,
                        ProComment: {
                            $cond: [{ $eq: ["$Action", "Comment"] }, 1, 0]
                        },
                        ProLike: {
                            $cond: [{ $eq: ["$Action", "like"] }, 1, 0]
                        }
                    }
                },
          {
              "$group": {
                  "_id": {
                      "$dateToString": {
                          "format": "%d-%m-%Y",
                          "date": {
                              "$add": [new Date(0), "$datetime"]
                          }
                      }
                  },
                  "CommentCount": { $sum: "$ProComment" },
                  "Likecount": { $sum: "$ProLike" }                     
              },
          },
          { '$sort': { '_id': 1 } } // here i have used sort but it's not work
            ]).exec(function (err, data) {
                if (err) {
                    console.log(err);
                }
                else {
                    console.log("Final=>", data);
                    res.send(data);
                }
            });

0 个答案:

没有答案