这里是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
}
]
我在哪里做错了。请帮助我。
声明'telecalling'在哪里?看起来在当前范围内没有对这个集合的引用。还要看看您使用的实际API。 ['.group()'](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#group)方法当然存在,但它的调用签名当然是不同的从shell实现。实际上你应该学习如何使用'.aggregate()',而不是依赖于JavaScript驱动的函数,除非完全不可能处理。这是一个简单的聚合。 –
@NeilLunn,我在顶部声明为var mongoose = require('mongoose'); var telecalling = mongoose.model('telecalling'); –
我的不好。错过了猫鼬标签。这使得它更简单。猫鼬模型没有'.group()'方法。您可以通过模型上的'.collection'访问器使用本地驱动程序方法,但是如前所述,您确实应该使用'.aggregate()'。所以这将是一个学习的好时机。 –