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

414
Visualizações
MongoDb how write aggregation query in java driver version 3.4

I'm not getting the way to write the equivalent query in java for driver version 3.4 of this aggregation query:

db.getCollection('MYTABLE').aggregate([ 
     {"$match": { "ID_STATUSMATRICULA": 1 } }, 
     {"$group": {
        "_id": null,
        "TOTAL" : { $sum: 1 },

        "MEDIA_APROVADOS": { 
            "$avg": {
                "$cond": [ { "$eq": [ "$ID_STATUSAPROVEITAMENTO", 1 ] }, "$NR_APROVEITAMENTO", 0, ]
            }
        },
        "MEDIA_REPROVADOS": { 
            "$avg": {
                "$cond": [ { "$eq": [ "$ID_STATUSAPROVEITAMENTO", 2 ] }, "$NR_APROVEITAMENTO", 0, ]
            }
        },
        "APROVADOS": { 
            "$sum": {
                "$cond": [ { "$eq": [ "$ID_STATUSAPROVEITAMENTO", 1 ] }, 1, 0, ]
            }
        },
        "REPROVADOS": { 
            "$sum": {
                "$cond": [ { "$eq": [ "$ID_STATUSAPROVEITAMENTO", 2 ] }, 1, 0, ]
            }
        },
        "PENDENTES": { 
            "$sum": {
                "$cond": [ { "$eq": [ "$ID_STATUSAPROVEITAMENTO", 0 ] }, 1, 0, ]
            }
        }
    }}
])

Anyone can help me with this ? I found many samples for old driver 2.2, but very poor documentation for 3.4 version.


SOLVED After Sagar Reddy answer, I can write the equivalent in java:

List pma = asList(new Document("$eq", asList("$ID_STATUSAPROVEITAMENTO", 1)), "$NR_APROVEITAMENTO", 0);
List pmr = asList(new Document("$eq", asList("$ID_STATUSAPROVEITAMENTO", 2)), "$NR_APROVEITAMENTO", 0);
List psa = asList(new Document("$eq", asList("$ID_STATUSAPROVEITAMENTO", 1)), 1, 0);
List psr = asList(new Document("$eq", asList("$ID_STATUSAPROVEITAMENTO", 2)), 1, 0);
List psp = asList(new Document("$eq", asList("$ID_STATUSAPROVEITAMENTO", 0)), 1, 0);

        Bson match = Aggregates.match(Filters.eq("ID_STATUSMATRICULA", 1));
        Bson group = Aggregates.group(null,
            sum("TOTAL", 1),
            avg("MEDIA_APROVADOS", computed("$cond", pma) ),
            avg("MEDIA_REPROVADOS", computed("$cond", pmr) ),
            sum("APROVADOS", computed("$cond", psa) ),
            sum("REPROVADOS", computed("$cond", psr) ),
            sum("PENDENTES", computed("$cond", psp) )
        );

        AggregateIterable<Document> cursor = mTable.aggregate(asList(match, group));
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can try something like. Add the rest of fields to the $group aggregation similar as MEDIA_APROVADOS

import static com.mongodb.client.model.Accumulators.*;
import static com.mongodb.client.model.Aggregates.*;
import static com.mongodb.client.model.Projections.*;
import static java.util.Arrays.asList;

Bson match = Aggregates.match(Filters.eq("ID_STATUSMATRICULA", 1));
Bson group = group(null,
                avg("MEDIA_APROVADOS", 
                computed("$cond", 
                         asList(new Document("$eq", asList("$ID_STATUSAPROVEITAMENTO", 1)), 
                        "$NR_APROVEITAMENTO", 0))
                 )
           );
collection.aggregate(asList(match, group));
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