Leonardo
Leonardo

Reputation: 159

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

Upvotes: 1

Views: 1458

Answers (1)

s7vr
s7vr

Reputation: 75994

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

Upvotes: 4

Related Questions