Aman
Aman

Reputation: 3261

Java code for mongodb aggregation query

I am new to Java. I checked few examples having aggregate usage.But still having few doubt.

 db.employee.aggregate({$unwind: '$dp.fin.Record'},       
 {$match:{"dp.mon":"patch.metrics",'dp.fin.Record':{$exists:1}}},      
 {$group:{_id: '$dp.fin.Record', count:{$sum:1}}},      
 {$project:{count:'$count'}},   {$group:{_id:'Total
 Count',total:{$sum:'$count'}}});

Can anybody help me writing java equivalent. I have doubt how can we pass multiple matching condition as I have shown above. I am trying like below

BasicDBObject unwind = new BasicDBObject("$unwind",
"$dp.fin.Record"); DBObject match = new BasicDBObject("$match", new
BasicDBObjectBuilder(""));

Upvotes: 0

Views: 3901

Answers (2)

Wizard
Wizard

Reputation: 4421

I think this will help:

DBObject unwind = new BasicDBObject("$unwind", "$dp.fin.Record");
DBObject match  = BasicDBObjectBuilder.start().push("$match")
                  .add("dp.mon", "patch.metrics")
                  .push("dp.fin.Record").add("$exists", true).get();
DBObject group1 = BasicDBObjectBuilder.start().push("$group")
                  .add("_id", "$dp.fin.Record")
                  .push("count").add("$sum", 1).get();
DBObject project= BasicDBObjectBuilder.start().push("$project")
                  .add("count", "$count").get();
DBObject group2 = BasicDBObjectBuilder.start().push("$group")
                  .add("_id", "Total Count")
                  .push("total").add("$sum", "$count").get();
// Suppose collection has been prepared
AggregationOutput aggr = collection.aggregate(Arrays.asList(unwind, match, group1, project, group2));

Upvotes: 3

Newbie
Newbie

Reputation: 1531

If you above query is correct. You could approach this below codes.

//Forming Unwind parts
DBObject unwind = new BasicObject("$unwind","$dp.fin.record");

//Forming Match parts
DBObject match = new BasicObject();
match.put("dp.mon","patch.metrics")
match.put("dp.fin.Record", new BasicDBObject("$exists",1));

//Forming Group parts
DBObject group1 = new BasicDBObject();
group1.put("_id", '$dp.fin.Record');
group1.put("count", new BasicDBObject("$sum", 1));

//Forming Project parts
DBObject project = new BasicDBObject();
project.put("count", '$count');

//Forming Group parts
DBObject group2 = new BasicDBObject();
group2.put("_id", 'Total Count');
group2.put("total", new BasicDBObject('$sum', '$count'));


/**
 * Executing aggregation 
 */
AggregationOutput output = mongoOperations.getCollection("employee").aggregate(unwind,
                                                                            new BasicDBObject("$match",match),
                                                                            new BasicDBObject("$group",group1),
                                                                            new BasicDBObject("$project",project),
                                                                            new BasicDBObject("$group",group2));

Hope this could help you.

Upvotes: 3

Related Questions