im trying to perform this aggregation in a mongo $lookup operation where i want to pass a variable down to a date object.
Wondering if this is possible
deal_cliff: new Date(new Date("$integrated").getTime() + 540 * 86400000),
as you can tell the problem is that "$integrated" is being used as a string and not an actual date since its a mongo dollar sign var.
any kind of help would be appreciated.
full code:
$lookup: {
from: 'deals',
// set variables that can be used in the pipeline below
let: {
group_id: '$parentGroupId',
internal_comm: { $toDecimal: '$internalCommission.amount' },
dealer_id: dealerId,
deal_cliff: new Date(new Date('$integrated').getTime() + 540 * 86400000), <---- Problem here
booking_provider: '$provider',
booking_booked_on: '$bookedOn'
},
pipeline: [
{
$match: {
$expr: {
$eq: ['$groupId', '$$group_id']
}
}
},
{ $unwind: '$dealers' },
...(userType === USER_TYPES.BDM && dealerId
? [
{
$match: {
$expr: {
$eq: ['$dealers.dealerId', '$$dealer_id']
}
}
}
]
: []),
{
$addFields: {
dealerComm: {
$cond: [
// If booking provider not Trivago & booking <= cliff date
{
$and: [
{ $ne: ['$$booking_provider', "Trivago"] },
{ $lte: ['$$booking_booked_on', "$$deal_cliff"] }
]
},
// do
{
$multiply: [
'$dealers.effort',
'$$internal_comm',
'$dealers.repCommission'
]
},
// else return
0
]
}
}
},
{
$group: {
_id: '$_id',
totalDealerComm: {
$sum: {
$cond: {
if: '$dealerComm',
then: { $toDecimal: '$dealerComm' },
else: 0
}
}
}
}
}
],
as: 'deal'
}