这是telecalling集合中的数据
{
"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不是一个函数。如果我在shell中执行此操作,我将结果显示为
[
{
"product" : "Fair Automobiles",
"enquiries" : 7,
"followups" : 15,
"installations" : 0,
"demos" : NaN
},
{
"product" : "Fair Fertilizers",
"enquiries" : 1,
"followups" : 0,
"installations" : 0
}
]
我在哪里做错了。请帮帮我。
答案 0 :(得分:1)
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
,它与查询参数完全相同。
接下来,由于您的操作相当简单,因此_id
使用"product"
.group()
密钥就像"demos"
一样。
$sum
字段具有逻辑值,您可以使用$group
运算符切换为数字值,然后传递给"actiondone"
。
实际的字段起初有点难以理解,但基本上你在数组上使用$cond
来查找与指定的var formData = {};
$('select[name="dropdwn"]').each(function() {
formData.dropdwn = $(this).val();
});
匹配的项目,然后使用$filter
来获取"尺寸"过滤后的列表并将其传递给$size
累加器进行计数。