[Solved] How to get mongoDB aggregated month wise data using java [closed]


Use the aggregation framework with 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 the example above, this outputs to 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, this can be implemented as 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);
    }

}

8

solved How to get mongoDB aggregated month wise data using java [closed]