2017-08-22 10 views
0

私のウェブサイトのカウンターレストapiから詳細の統計情報を取得しようとしています。データベースの構造は次のとおりです。私はノードに慣れていません。Node.js mongoose REST集約

bear.IP = req.body.IP; 
bear.CountryName = req.body.CountryName; 
bear.CountryCode = req.body.CountryCode; 
bear.ISP = req.body.ISP; 
bear.City = req.body.City; 
bear.Os = req.body.Os; 
bear.Date = req.body.Date; 

すべてのページロードのRESTへのPOST要求。
Iは総ビューはカウント得ることができる - 私が取得したい

router.use('/counter/total', function (req, res) { 
    Bear.distinct('_id').count().exec(function (err, viewcount) { 
     res.json(viewcount); 
    }); 
}); 

である({)COUNTRYNAME(個別:再生回数}) - または(DISTINCT {(OS):再生回数})このような類似の集約。しかし、私はそれを作ることができません。

必要な出力は次のようなものです - 私はこの問題を解決するにはどうすればよい

[{ 
    "United Kingdom": "121345" 
}, { 
    "Australia": "18645" 
}, { 
    "Myanmar": "297" 
}, { 
    "United States": "17212" 
}, ... 

。親切に私を助けてください。ありがとう。

小さなサンプルDB -

[{"_id":"597c802a33dc398272000001","Os":"Windows","FingerPrint":"3185454076","City":"Yangon","ISP":"Telenor Myanmar","CountryCode":"MM","CountryName":"Myanmar [Burma]","IP":"37.111.1.237","__v":0},{"_id":"597c802a33dc398272000002","Os":"Windows","FingerPrint":"1711566246","City":"Kajang","ISP":"TM Net","CountryCode":"MY","CountryName":"Malaysia","IP":"60.48.94.211","__v":0},{"_id":"597c802e33dc398272000003","Os":"Android","FingerPrint":"3338867125","City":"Yangon","ISP":"Tech Solutions","CountryCode":"MM","CountryName":"Myanmar [Burma]","IP":"74.50.208.220","__v":0},{"_id":"597c802e33dc398272000004","Os":"Android","FingerPrint":"4170065776","City":"Kuala Lumpur","ISP":"U Mobile Sdn Bhd","CountryCode":"MY","CountryName":"Malaysia","IP":"123.136.106.229","__v":0},{"_id":"597c803033dc398272000005","Os":"Android","FingerPrint":"1276175308","City":"Kuala Lumpur","ISP":"TM Net","CountryCode":"MY","CountryName":"Malaysia","IP":"175.140.188.42","__v":0},{"_id":"597c803033dc398272000006","Os":"Android","FingerPrint":"585184694","City":"Bangkok","ISP":"True Internet","CountryCode":"TH","CountryName":"Thailand","IP":"171.99.162.51","__v":0},{"_id":"597c803033dc398272000007","Os":"Android","FingerPrint":"4149392815","City":"Shah Alam (Seksyen 11)","ISP":"DiGi Telecommunications Sdn. Bhd.","CountryCode":"MY","CountryName":"Malaysia","IP":"115.164.88.199","__v":0},{"_id":"597c803333dc398272000008","Os":"Android","FingerPrint":"2143955121","City":"Kuala Lumpur","ISP":"Maxis Communications","CountryCode":"MY","CountryName":"Malaysia","IP":"113.210.66.130","__v":0},{"_id":"597c803333dc398272000009","Os":"Android","FingerPrint":"2914318643","City":"Yangon","ISP":"Telenor Myanmar","CountryCode":"MM","CountryName":"Myanmar [Burma]","IP":"37.111.15.178","__v":0},{"_id":"597c803533dc39827200000a","Os":"Android","FingerPrint":"1819736779","City":"Puchong Batu Dua Belas","ISP":"TM Net","CountryCode":"MY","CountryName":"Malaysia","IP":"124.13.138.153","__v":0},{"_id":"597c803533dc39827200000b","Os":"Android","FingerPrint":"1178799827","City":"Yangon","ISP":"Myanma Posts and Telecommunications","CountryCode":"MM","CountryName":"Myanmar [Burma]","IP":"103.52.14.108","__v":0},{"_id":"597c803733dc39827200000c","Os":"Android","FingerPrint":"3933020417","City":"Kuala Lumpur","ISP":"U Mobile Sdn Bhd","CountryCode":"MY","CountryName":"Malaysia","IP":"123.136.112.220","__v":0},{"_id":"597c803833dc39827200000d","Os":"Android","FingerPrint":"4079283873","City":"Loikaw","ISP":"Myanma Posts and Telecommunications","CountryCode":"MM","CountryName":"Myanmar [Burma]","IP":"103.52.12.167","__v":0},{"_id":"597c803933dc39827200000e","Os":"Android","FingerPrint":"3722986014","City":"Bangkok","ISP":"3BB Broadband","CountryCode":"TH","CountryName":"Thailand","IP":"27.130.160.142","__v":0},{"_id":"597c803a33dc39827200000f","Os":"Android","FingerPrint":"2595883208","City":"Yangon","ISP":"Fortune International","CountryCode":"MM","CountryName":"Myanmar [Burma]","IP":"103.215.194.7","__v":0},{"_id":"597c803b33dc398272000010","Os":"Android","FingerPrint":"3844048617","City":"Parit Raja","ISP":"Ytl Communications Sdn Bhd","CountryCode":"MY","CountryName":"Malaysia","IP":"183.78.126.125","__v":0},{"_id":"597c803b33dc398272000011","Os":"Windows","FingerPrint":"2163751345","City":"Singapore","ISP":"MobileOne","CountryCode":"SG","CountryName":"Singapore","IP":"138.75.196.239","__v":0},{"_id":"597c803e33dc398272000012","Os":"Android","FingerPrint":"475903491","City":"Songkhla","ISP":"3BB Broadband","CountryCode":"TH","CountryName":"Thailand","IP":"171.7.236.10","__v":0},{"_id":"597c803f33dc398272000013","Os":"Android","FingerPrint":"999220323","City":"Yangon","ISP":"Myanma Posts and Telecommunications","CountryCode":"MM","CountryName":"Myanmar [Burma]","IP":"45.112.176.80","__v":0}] 
+0

あなたは、あなたのサンプル・データベースは、最初のように見えるしてください示してもらえてみてください? –

+0

@ WildanS.Nahar、はい、質問に追加しました。ありがとう。 –

答えて

1

aggregation exampleに続いて、多分

router.get('/counter/total', function (req, res) { 
    Bear.aggregate([ 
     { 
      $group: { 
       _id: "$CountryName" 
       viewcount: { $sum: 1 } 
      } 
     } 
    ], function (err, bears) { 
     console.log(err, bears); 
     // remap the results 
     var viewcounts = bears.map(function (bear) { 
      // using ES6 to compute property name 
      return { [bear._id]: bear.viewcount }; 
     }); 
     console.log(viewcounts); 
    }); 
}); 
+0

私にとって素晴らしいソリューションです。ありがとう。 –

関連する問題