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

194
Views
I want to group by month and get sum of amount from project embedded document

This is my mongo-DB database collection image [1]: https://i.stack.imgur.com/TIKh1.png

I want to add query in which I need to get group by month and sum of (amount) from (project) array that is being embedded in (usermodel) collection

This is how i am currently doing it, but in console no output is showing

router.get('/calculatedata', Authenticate, async (req, res) => {
    try {
        const getUser = await UserModel.findById({ _id: req.userID });
        const getProject = await getUser.aggregate([
            {$unwind: '$project' },
            {$group: { _id: { month: "$month" },
                      total:{$sum:"$project.amount"}
                     }
            }]);
        console.log(getProject)
    } catch (error) {
        res.status(400).send({ message: "Something Went Wrong" })
    }
})
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to extract the month from date and extract the date from string first , example:

 month: {$month: { $dateFromString:{ dateString:"$project.date" , timezone:'America/New_York' }} }

or extract the month like number ( 01..12) from the string:

    month: {  $substr: ["$project.date", 5, 7]     }
about 4 years ago · Juan Pablo Isaza Report
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!