Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

103
Views
pass mongo dollar sign aggregation var to date object in javascript

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'
    }
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!