How to get mongoDB aggregated month wise data using java -


here data in db.

"accounts" : [          {             "total_credits" : 4000,             "total_debits" : 0,             "date" : "25-05-2015"         },          {             "total_credits" : 1000,             "total_debits" : 0,             "date" : "26-05-2015"         },          {             "total_credits" : 1000,             "total_debits" : 0,             "date" : "10-07-2015"         }] 

i want extract sum of total credits , debits month wise. want in java.

use aggregation framework following aggregation pipeline (mongo shell implementation):

db.ledger.aggregate([     {         "$unwind": "$accounts"     },     {         "$project": {             "total_credits" : "$accounts.total_credits",             "total_debits" : "$accounts.total_debits",             "month_year" : {  "$substr": [ "$accounts.date", 3, -1 ] }         }     },     {         "$group": {             "_id": "$month_year",             "total_credits": { "$sum": "$total_credits" },             "total_debits": { "$sum": "$total_debits" }         }     } ]) 

with example above, outputs console:

/* 0 */ {     "result" : [          {             "_id" : "07-2015",             "total_credits" : 1000,             "total_debits" : 0         },          {             "_id" : "05-2015",             "total_credits" : 5000,             "total_debits" : 0         }     ],     "ok" : 1 } 

with java, can implemented follows:

import com.mongodb.aggregationoutput; import com.mongodb.basicdbobject; import com.mongodb.db; import com.mongodb.dbcollection; import com.mongodb.dbobject;  public class aggregation {      public static void main(string[] args) {          db db = mongodb.getaccountsdb();         dbcollection ledger = db.getcollection("ledger");          //------------------------------------------------- aggregation framework          dbobject unwind = new basicdbobject("$unwind", "$accounts");         list<object> substrlist = arrays.aslist(new object[]{"$accounts.date", 3, -1});         dbobject monthprojection = new basicdbobject("$substr", substrlist);         dbobject projectfields = new basicdbobject("total_credits", "$accounts.total_credits");         projectfields.put("total_debits", "$accounts.total_debits");         projectfields.put("month_year", monthprojection);         dbobject project = new basicdbobject("$project", projectfields );          dbobject groupfields = new basicdbobject( "_id", "$month_year");         groupfields.put("total_credits", new basicdbobject( "$sum", "$total_credits"));         groupfields.put("total_debits", new basicdbobject( "$sum", "$total_debits"));         dbobject group = new basicdbobject("$group", groupfields);          aggregationoutput output = ledger.aggregate( unwind, project, group );          system.out.println("\n" + output);     }  } 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -