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

139
Vistas
Is it possible to round a number with Spring data MongoDB aggregations?

I have the following aggregation pipeline to calculate the top rated brands from a collection of phones with their reviews embedded.

public Document findTopRatedBrands(int minReviews, int results) {
        UnwindOperation unwindOperation = unwind("reviews");
        GroupOperation groupOperation = group("$brand").avg("$reviews.rating")
                        .as("avgRating").count().as("numReviews");
        MatchOperation matchOperation = match(new Criteria("numReviews").gte(minReviews));
        SortOperation sortOperation = sort(Sort.by(Sort.Direction.DESC, "avgRating",
                "numReviews"));
        LimitOperation limitOperation = limit(results);
        ProjectionOperation projectionOperation = project().andExpression("_id").as
                ("brand").andExpression("avgRating").as("rating").andExclude("_id")
                .andExpression("numReviews").as("reviews");
        Aggregation aggregation = newAggregation(unwindOperation, groupOperation, matchOperation,
                sortOperation, limitOperation, projectionOperation);
        AggregationResults<Phone> result = mongoOperations
                .aggregate(aggregation, "phones", Phone.class);
        return result.getRawResults();
    }

An example of document in the phones collection is this:

{
    "_id": {
        "$oid": "61e1cc8f452d0aef89d9125f"
    },
    "brand": "Samsung",
    "name": "Samsung Galaxy S7",
    "releaseYear": 2016,
    "reviews": [{
        "_id": {
            "$oid": "61d4403b86913bee0245c171"
        },
        "rating": 2,
        "dateOfReview": {
            "$date": "2019-12-24T00:00:00.000Z"
        },
        "title": "Won't do that again.",
        "body": "I could not use with my carrier. Sent it back.",
        "username": "bigrabbit324"
    }]
} 

I would like to sort by avgRating (rounded to the first decimal place), and secondly by the number of reviews. Now the average rating it's not rounded so it gives always different values, so I can't sort by number of reviews also. I have seen the ArithmeticOperators.Round class but I don't understand how to include it here if possible.

An example of result is the following:

[Document{{brand=Nokia, rating=3.25, reviews=4}}] 

I would like to have 3.2 as rating.

This works in Mongo Compass:

    $project: {
        _id: 0,
        brand: '$_id',
        rating: { $round: ['$avgRating', 1] }
    }
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

try

ProjectionOperation roundAverageRating = Aggregation.project("avgRating", "numReviews")
    .and(ArithmeticOperators.Round.roundValueOf("avgRating").place(1))
    .as("avgRatingRounded");
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