Skip to content

Instantly share code, notes, and snippets.

@renderlife
Last active May 15, 2021 16:56
Show Gist options
  • Save renderlife/01e4acdad2cfcad4064a8ff6a5c3e819 to your computer and use it in GitHub Desktop.
Save renderlife/01e4acdad2cfcad4064a8ff6a5c3e819 to your computer and use it in GitHub Desktop.
agregate.json
{
$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