Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

189
Visualizações
MongoDB - Sum aggregation with conditionals

I am working on aggregation to perform some analytics on an Orders object, I want to get the total price from every order from the previous month, and the total from this month, I tried passing in the $match parameter, with the appropriate time conditionals but it's not aggregating any of the data. even though if I perform a countDocuments query with the same time conditionals I get a number of documents returned.

const totalThis = await Order.aggregate([
  {
    $match: {
      // should cut the amount of orders off at the beginning of the current month
      createdAt: { $gte: thisMonth.getMonth() + 1 },
    },
  },
  {
    $group: {
      // using $group, _id has to be there or it doesnt
      // return anything, though we dont need an _id
      _id: null,
      total: {
        $sum: "$totalPrice",
      },
    },
  },
]);
res.status(200).json({
  lastMonth: lastMonthSales,
  thisMonth: thisMonthSales,
  totalSalesLast: totalLast,
  totalSalesThis: totalThis,
});

In my Postman request, lastMonth returns 0, so it would make sense that the array returned to totalSalesLast would be empty, however thisMonth returns 5 documents, so it should work. Obviously, if I remove the $match conditional it will aggregate all the documents, but that's because it's doing ALL the documents and not actually filtering.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

To query the document with createdAt in specific month, your $match stage should be as below:

  1. $month - Take the month value from createdAt.
  2. $eq - Compare both month from (1) and input month are same.
{
  $match: {
    $expr: {
      $eq: [
        {
          "$month": "$createdAt"
        },
        thisMonth.getMonth() + 1
      ]
    }
  }
}

Sample Mongo Playground

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda