2017-10-11 65 views
0

這裏我已經爲圖表中的顯示計數日期明智地創建了一個查詢。我已經使用了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);   
} 
}) 

任何人如何能做到那麼請讓我知道。

+0

像'{ 「$匹配」:{ 「_id」:物件(「595e3b2033961713940442cf」),「History.datetime」:{「$ lte」:1507529239000,「$ gte」:150701228555 8},History.Action:{$ in:[「Comment」,「like」]}}}'對於兩個匹配階段 – Veeram

+0

是我嘗試但返回相同的o/p – Edit

+0

如何在o /對於喜歡? – Edit

回答

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 
     ] 
    } 
    } 
} 
+0

感謝好友.... – Edit

+0

你好,我正在與現在的對象,我正在使對象的新查詢然後我收到錯誤「日期不定義」,所以如何解決它? – Edit

+0

使用相同的o/p – Edit

相關問題