Trying to get the count of various statuses & count of overdue items for each user. Below is the sample document:
[{
_id:"12324332432",
caseInd:"TASK123",
TaskId:"12345",
dueDate:ISODate("2021-03-22T00:00:00Z"),
assingee:"ABC",
status:"IN_PROGRESS",
completionDate:null
},
{
_id:"12324332433",
caseInd:"TASK124",
TaskId:"12345",
dueDate:ISODate("2021-03-01T00:00:00Z"),
assingee:"CDE",
status:"IN_PROGRESS",
completionDate:null
},
{
_id:"12324332434",
caseInd:"TASK125",
TaskId:"12345",
dueDate:ISODate("2021-03-31T00:00:00Z"),
assingee:"ABC",
status:"COMPLETED",
completionDate:ISODate("2021-03-01T00:00:00Z")
}...]
Using aggregation, I want to get the count of tasks (completed,InProgress & overdue) for each user. like
[{
_id : ABC ,
data: [
{status:"Completed", count:15},
{status:"InProgress", count:3},
{status:"Overdue", count:3}]
},{
_id : CDE,
data: [
{status:"Completed", count:5},
{status:"InProgress", count:1},
{status:"Overdue", count:0}]
}]
I have tired this solution, but not sure how to include overdue count.
db.colName.aggregate([ {$project: {_id:0,
dueDateIn:{$divide:[{$subtract:[new Date(),$dueDate]},86400000]},
status:1,
assignee:1
}
}, {
$group:{
_id:{"assignee":"$assignee", "status":"$status"},
count:{$sum:1}
},
$group:{
_id:"$_id.assignee" , result:{$push:{"status":"$_id.status", count:"$count"}}
}
}]) ```
Any help would be highly appreciated. Thank you.
Take a look at the custom $accumulator in the $group pipeline.
After you group the documents with _id
field, you can use the custom accumulator to count the status.
$group https://docs.mongodb.com/manual/reference/operator/aggregation/group/#pipe._S_group
$accumulator https://docs.mongodb.com/manual/reference/operator/aggregation/accumulator/#grp._S_accumulator
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.