如何处理多个动作并使用mongodb进行计数?

时间:2017-10-11 06:34:21

标签: mongodb

这里我已经为图表中的节目计数日期创建了一个查询。我用过mongodb。我有一个对象,并在对象存储多个数据,并用一个值行动分开。我想显示计数日期明智行动像评论,像动作计数想要显示。现在我只是显示评论数。我也想得到数数。 我在下面列出了我的对象和查询。

这是我的对象数组=>

{
   "_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
 }

我当前的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 } ]

我的例外情况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},     

这是我的查询=>

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);        
}
 })

任何人如何做到那么请告诉我。

1 个答案:

答案 0 :(得分:1)

先将$match更新为

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

和第二个$match

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

$group

{
  "likecount": {
    "$sum": {
      "$cond": [
        {
          "$eq": [
            "$History.Action",
            "like"
          ]
        },
        1,
        0
      ]
    }
  }
}