使用mongodb通过日期获取错误数据使用Group?

时间:2017-11-01 09:54:34

标签: mongodb

这里我已经为图表中的节目计数日期创建了一个查询。我用过mongodb。我有一个表集合,并在表集合中存储多个文档,在文档中一个归档是Action。我想显示计数日期明智行动像评论,像动作计数想要show.but我创建一个查询使用group by但我返回查询中的所有文档没有在查询中获得日期明智的过滤数据。我不知道在查询中我的错误在哪里,任何人都知道,请告诉我。 我想要从开始到结束日期之间的日期。

这是我的文件=>

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

我当前的o / p =>

  { _id: '1507020728000', CommentCount: 1 ,Likecount:0},
  { _id: '1507099928000', CommentCount: 1 ,Likecount:0},
  { _id: '1507099928000', CommentCount: 1 ,Likecount:0},
  { _id: '1507186328000', CommentCount: 1,Likecount:0 },
  { _id: '1507186328000', CommentCount: 1,Likecount:0 },
  { _id: '1507279928000', CommentCount: 1,Liekcount:1},
  { _id: '1507279928000', CommentCount: 1,Liekcount:1},
  { _id: '1504256404000', CommentCount: 0,Liekcount:1}, 

我的例外情况o / p =>

  { _id: '1507020728000', CommentCount: 1 ,Likecount:0},
  { _id: '1507099928000', CommentCount: 2 ,Likecount:0},
  { _id: '1507186328000', CommentCount: 2,Likecount:0 },
  { _id: '1507279928000', CommentCount: 2,Liekcount:1}, 

这是我的查询=>

            ActivityHistory.aggregate([
          {
              "$match": {
                  "InId": ObjectId("595e3b2033961713940442cd"),                     
                  "datetime": {
                      "$lte": parseInt("1507280404000"), "$gte": parseInt("1507021204000")
                  },
                  "Action": {                          
                      $in: ["Comment", "Like"]
                  }
              }
          },      
          {              
              "$group": {
                  "_id": "$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("Final=>",data);            
                }
        });

0 个答案:

没有答案