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

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

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 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