Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

197
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda