这是电话收集中的数据
{
"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",
}
在这里我需要按产品分组,并获得完成了多少演示、查询、跟进和安装。
这是控制器
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);
}
});
}
我收到 TypeError: telecalling.group is not a function。如果我在 shell 中执行这个,我得到的结果是
[
{
"product" : "Fair Automobiles",
"enquiries" : 7,
"followups" : 15,
"installations" : 0,
"demos" : NaN
},
{
"product" : "Fair Fertilizers",
"enquiries" : 1,
"followups" : 0,
"installations" : 0
}
]
我哪里做错了。请帮帮我。
Mongoose 模型没有.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
与查询参数完全相同的。
接下来,由于您的操作相当简单,因此您可以像您一样$group
使用 的_id
键进行单个阶段。"product"
.group()
该"demos"
字段有一个逻辑值,您可以使用$cond
运算符将其切换为数字,然后传递给$sum
。
实际字段一开始有点难以理解,但基本上您可以$filter
在数组上使用来查找与指定 匹配的项目"actiondone"
,然后使用$size
来获取过滤列表的“大小”并将其传递给$sum
累加器进行计数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句