Last active
May 15, 2021 16:56
-
-
Save renderlife/01e4acdad2cfcad4064a8ff6a5c3e819 to your computer and use it in GitHub Desktop.
agregate.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
$match: { | |
'company': ObjectId(companyId), | |
'status': { | |
'$eq': status | |
}, | |
'dateShift': { | |
'$gte': from | |
}, | |
'dateShift': { | |
'$lte': to | |
} | |
} | |
}, | |
{ | |
$lookup: { | |
from: 'work_shifts', | |
localField: '_id', | |
foreignField: 'order', | |
as: 'work_shifts' | |
} | |
}, | |
{ | |
$lookup: { | |
from: 'companies', | |
localField: 'company', | |
foreignField: '_id', | |
as: 'company' | |
} | |
}, | |
{ | |
$project: { | |
orderId: 1, | |
dateShift: 1, | |
company: { | |
$first: '$company.name' | |
}, | |
work_shifts: 1 | |
} | |
}, | |
{ | |
$unwind: { | |
path: '$work_shifts' | |
} | |
}, | |
{ | |
$lookup: { | |
from: 'employees', | |
localField: 'work_shifts.employee', | |
foreignField: '_id', | |
as: 'employees' | |
} | |
}, | |
{ | |
$project: { | |
orderId: 1, | |
dateShift: 1, | |
company: 1, | |
'total': '$work_shifts.workShiftTotal', | |
'work_shifts_summary': { | |
rate: '$work_shifts.workShiftRate', | |
amount: '$work_shifts.workShiftAmount', | |
total: '$work_shifts.workShiftTotal' | |
}, | |
employee: { | |
name: {$first: '$employees.name'}, | |
lastName: {$first: '$employees.lastName'}, | |
employeeId: {$first: '$employees.employeeId'} | |
} | |
} | |
}, | |
{ | |
$group: { | |
"_id": { | |
"orderId": "$orderId" | |
}, | |
"work_shifts": { | |
"$addToSet": "$work_shifts_summary" | |
}, | |
"total": { | |
"$sum": "$total" | |
}, | |
"count": { | |
"$sum": 1 | |
}, | |
} | |
}, | |
{ | |
$project: { | |
"_id": 0, | |
orderId: "$_id.orderId", | |
total: 1, | |
count: 1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment