2017-10-10 69 views
1

我想使用數據顯示數據在圖表中使用明智的最後7天數據。我使用這個數據庫是mongodb和在nodejs中工作。我也創建了一個查詢生成日期智者,但我沒有得到成功。我正在計數整個日期明智。不按日期申請。並在數據庫中我有存儲日期時間的時間戳,所以我需要在這裏工作只有日期。我也有最後7天生成和生成時間戳lastdate和應用一個條件lastdate大於或等於日期以獲得計數。所以在這裏我列出了我的代碼和對象,所以任何看到並有想法如何解決它,然後請讓我知道。按日期分組並使用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: null, count: 8 } 

我expecated O/P =>

{ _id: 3-10-2017, count: 1,Action = "comment" } 
{ _id: 4-10-2017, count: 2,Action = "comment" } 
{ _id: 5-10-2017, count: 2,Action = "comment" } 
{ _id: 6-10-2017, count: 2,Action = "comment" } 

這裏,這是我的查詢=>

var lastdate = 1507012285558; // this is last date 3rd oct 2017 now here to greate than date find and get count 

    InstaAc.aggregate([ 
         { 
          "$match": { 
           "_id": ObjectId("595e3b2033961713940442cf"), 
           "History.datetime": { 
            "$gte": lastdate 
           } 
          } 
         }, 
         { 
          "$unwind": "$History" 
         }, 
         { 
          "$match": { 
           "History.datetime": { 
            "$gte": lastdate 
           }, 
           "History.Action": { 
            $eq: "comment" 
           } 
          } 
         },     
         { 
          "$group": { 
           "_id": "$_id",  
           "count": { 
            "$sum": 1 
           }, 
           "History": { 
            "$push": "$History" 
           } 
          } 
         }, 
         { 
          "$sort": { 
           "_id": 1 
          } 
         } 
         ]).exec(function (err, data) { 
          if (err) { 
           console.log(err); 
          } 
          else {         
           console.log(data[0]);         
          } 
         }) 
+0

的可能的複製[多麼明智使用組從MongoDB中數組對象來獲取盤點日期?(https://stackoverflow.com/questions/46617530/how-to -get-count-date-wise-using-group-by-the-array-object-in-mongodb) – Veeram

回答

-1

查詢您必須修改這一行

{ 
    "$group": { 
     "_id": {datetime:"$datetime",action:"$Action"}, 
     "count": { 
      "$sum": 1 
     }, 
    } 
} 
在這裏

你要組dateandtime也行動明智

和你有你的時間戳轉換成新的Date對象的數組

{ 
    "_id" : ObjectId("578fa05a7391bb0d34bd3c28"), 
    "Action" : "Comment", 
    "datetime" :new Date(1507099928000) 
}, 

所得到的輸出

{ 
    "_id" : ObjectId("578fa05a7391bb0d34bd3c28"), 
    "Action" : "Comment", 
    "datetime" : ISODate("2017-10-04T06:52:08.000Z") 
} 

投影,你可以包括像

{ 
    "$project": { 
     _id:0, 
     "yearMonthDay": { 
      $dateToString: { 
       format: "%d-%m-%Y", 
       date: "$_id.datetime" 
      } 
     }, 
     action:"$_id.action", 
     count:1 
    } 
} 
+0

你能不能在這篇文章中給我多一點點提示 – Edit

+0

我得到這樣的錯誤「can '從BSON類型雙轉換爲日期「 – Edit

+0

你可以給我你在日期轉換編寫的代碼? –