如何使用 java 获取 mongoDB 汇总的月份数据
How to get mongoDB aggregated month wise data using java
这是我在数据库中的数据。
"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"
}]
我想按月提取总贷方和借方的总和。
我想在 java.
完成
使用 aggregation framework 和以下聚合管道(Mongo shell 实现):
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" }
}
}
])
对于上面的例子,这输出到控制台:
/* 0 */
{
"result" : [
{
"_id" : "07-2015",
"Total_Credits" : 1000,
"Total_Debits" : 0
},
{
"_id" : "05-2015",
"Total_Credits" : 5000,
"Total_Debits" : 0
}
],
"ok" : 1
}
使用Java,可以实现如下:
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);
}
}
这是我在数据库中的数据。
"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"
}]
我想按月提取总贷方和借方的总和。 我想在 java.
完成使用 aggregation framework 和以下聚合管道(Mongo shell 实现):
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" }
}
}
])
对于上面的例子,这输出到控制台:
/* 0 */
{
"result" : [
{
"_id" : "07-2015",
"Total_Credits" : 1000,
"Total_Debits" : 0
},
{
"_id" : "05-2015",
"Total_Credits" : 5000,
"Total_Debits" : 0
}
],
"ok" : 1
}
使用Java,可以实现如下:
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);
}
}