2017-05-20 40 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

非常感谢。 –

相关问题