2017-05-20 43 views
0

這裏是telecalling收集類型錯誤:collection.group是不是在貓鼬的功能

{ 
    "product": "a", 
    "demo": true, 
    "followups": [ 
     { 
      "followup": "2017-05-03T07:54:41.085Z", 
      "actiondone": "enquiry" 
     }, 
     { 
      "followup": "2017-05-05T07:54:41.085Z", 
      "actiondone": "followup" 
     } 
    ], 
    "createdAt": "2017-05-03T07:54:41.085Z", 
}, 
{ 
    "product": "b", 
    "demo": false, 
    "followups": [ 
     { 
      "followup": "2017-05-04T07:54:41.085Z", 
      "actiondone": "followup" 
     }, 
     { 
      "followup": "2017-05-10T07:54:41.085Z", 
      "actiondone": "installation" 
     } 
    ], 
    "createdAt": "2017-05-04T07:54:41.085Z", 
}, 
{ 
    "product": "a", 
    "demo": false, 
    "followups": [ 
     { 
      "followup": "2017-05-06T07:54:41.085Z", 
      "actiondone": "followup" 
     } 
    ], 
    "createdAt": "2017-05-06T07:54:41.085Z", 
} 

在這裏,我需要按產品,並得到多少演示,諮詢,followups和裝置完成的數據。

以下是一個

var mongoose = require('mongoose'); 
var telecalling = mongoose.model('telecalling'); 
summaryReport: function(request,response){ 
     telecalling.group({ 
      key: {product: 1}, 
      cond: {"createdAt": {"$gte": new Date(request.body.fromdate),"$lte": new Date(request.body.todate)}}, 
      reduce: function(curr, result) { 
       if(curr.demo==true){ 
        result.demos++; 
       } 
       var fups = curr.followups; 
       fups.forEach(allCounts); 
       function allCounts(fup){ 
        var action = fup.actiondone.toLowerCase() 
        if(action=='enquiry'){ 
         result.enquiries++; 
        } 
        if(action=='followup'){ 
         result.followups++; 
        } 
        if(action=='installation'){ 
         result.installations++; 
        } 
       } 
      }, 
      initial: {enquiries: 0, followups: 0, installations: 0} 
     }, function(err,res){ 
      if(err){ 
       response.json(err); 
      } 
      else{ 
       response.json(res); 
      } 
     }); 
} 

我得到類型錯誤控制器:telecalling.group不是一個函數。如果我在shell中執行此操作,我會得到結果爲

[ 
    { 
      "product" : "Fair Automobiles", 
      "enquiries" : 7, 
      "followups" : 15, 
      "installations" : 0, 
      "demos" : NaN 
    }, 
    { 
      "product" : "Fair Fertilizers", 
      "enquiries" : 1, 
      "followups" : 0, 
      "installations" : 0 
    } 
] 

我在哪裏做錯了。請幫助我。

+0

聲明'telecalling'在哪裏?看起來在當前範圍內沒有對這個集合的引用。還要看看您使用的實際API。 ['.group()'](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#group)方法當然存在,但它的調用簽名當然是不同的從shell實現。實際上你應該學習如何使用'.aggregate()',而不是依賴於JavaScript驅動的函數,除非完全不可能處理。這是一個簡單的聚合。 –

+0

@NeilLunn,我在頂部聲明爲var mongoose = require('mongoose'); var telecalling = mongoose.model('telecalling'); –

+0

我的不好。錯過了貓鼬標籤。這使得它更簡單。貓鼬模型沒有'.group()'方法。您可以通過模型上的'.collection'訪問器使用本地驅動程序方法,但是如前所述,您確實應該使用'.aggregate()'。所以這將是一個學習的好時機。 –

回答

1

貓鼬模型沒有.group()方法。對於大多數情況,你應該使用.aggregate(),所以值得學習。

繼承人的等價操作:

telecalling.aggregate([ 
    { "$match": { 
    "createdAt": { 
     "$gte": new Date(request.body.fromdate), 
     "$lte": new Date(request.body.todate) 
    } 
    }} 
    { "$group": { 
    "_id": "$product", 
    "demos": { 
     "$sum": { 
     "$cond": { 
      "if": { "$eq": [ "$demo", true ] }, 
      "then": 1, 
      "else": 0 
     } 
     } 
    }, 
    "enquries": { 
     "$sum": { 
     "$size": { 
      "$filter": { 
      "input": "$followups", 
      "as": "f", 
      "cond": { "$eq": [ "$$f.actiondone", "enquiry" ] } 
      } 
     } 
     } 
    }, 
    "followups": { 
     "$sum": { 
     "$size": { 
      "$filter": { 
      "input": "$followups", 
      "as": "f", 
      "cond": { "$eq": [ "$$f.actiondone", "followup" ] } 
      } 
     } 
     } 
    }, 
    "installations": { 
     "$sum": { 
     "$size": { 
      "$filter": { 
      "input": "$followups", 
      "as": "f", 
      "cond": { "$eq": [ "$$f.actiondone", "installation" ] } 
      } 
     } 
     } 
    } 
    }} 
],function(err,response) { 
    // work with result 
}) 

首先,你有一個$match這是完全一樣的查詢參數。

下一頁,因爲你的操作是相當簡單的,你必須使用"product"_id鍵單$group階段就像你的.group()一樣。

"demos"字段有一個邏輯值,您可以使用$cond運算符使用數字切換,然後傳遞給$sum

實際字段是有點棘手,首先要了解,但基本上你用$filter陣列上找到匹配指定"actiondone"的項目,然後用$size得到過濾列表,並通過的「大小」說到$sum累加器進行計數。

+0

非常感謝。它工作完美。你能告訴我如何在同一個查詢中獲得演示計數(這只是真實的)。 –

+0

@ G.Mounika它非常簡單。編輯列表以包括現在。 –

+0

非常感謝。 –

相關問題