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

230
Visualizações
How to sum a mongodb inner field and push it during grouping using MongoTemplate

I can use the sum within the push operation in a MongoDB console. However, I am not getting how can I do the same using MongoTemplate?

$group : {
     _id: "$some_id",
     my_field: { $push : {$sum: "$my_field" }}
 }

The code I am using for this is something like:

Aggregation aggregation =
    Aggregation.newAggregation(
        match(matchingCriteria),
        group("some_id")
            .count()
            .as("count")
            .push("my_field")
            .as("my_field")
        project("some_id", "count", "my_field"));
AggregationResults<MyModel> result =
    mongoTemplate.aggregate(aggregation, "my_collection", MyModel.class);

The thing is I want the sum of my_field but it is coming as an array of my_field here(as I am directly using the push). I am able to achieve the same using the above sum inside of push operation. But not able to use that for MongoTemplate. My app is in Spring Boot. I have also looked into the docs for these methods but couldn't find much.

Also, I tried directly using .sum() as well on the field(without using the push), but that is not working for me as my_field is an inner object, and it's not a number but an array of numbers after the grouping. That is why I need to use the push and sum combination.

Any help regarding this is appreciated. Thanks in advance.

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

0

I was able to get this to work using the below code:

Aggregation aggregation =
    Aggregation.newAggregation(
        match(allTestMatchingCriteria),
        project("some_id")
            .and(AccumulatorOperators.Sum.sumOf("my_field"))
            .as("my_field_sum")
        group("some_id")
            .count()
            .as("count")
            .push("my_field_sum")
            .as("my_field_sum"),
        project("some_id", "count", "my_field_sum"));
AggregationResults<MyModel> result =
    mongoTemplate.aggregate(aggregation, "my_collection", MyModel.class);

I used AccumulatorOperators.Sum in the projection stage itself and sum the inner fields and get the desired output. Then I passed this to the grouping stage where I did the count aggregation as I needed that data as well and then had to project all the data generated to be collected as output.

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