如何使用document和mongo聚合将mongo shell代码转换为java代码

时间:2016-04-22 07:36:04

标签: java mongodb mongodb-query aggregation-framework

如何将此代码转换为java?如何在java中使用聚合框架?任何人都可以帮助我...

我的java代码:

Document update = new Document("$project",new Document("TOTAL_EMPLOYEE_SALARY",new Document("$sum","$employees.EMP_SALARY")));
 AggregationOutput output = coll.aggregate(update); // throwing some error in eclipse

我的mongo shell代码:

db.collection.aggregate([
    { "$project": {
        "TOTAL_EMPLOYEE_SALARY": {
           "$sum": "$employees.EMP_SALARY"
        }
    }}
])

1 个答案:

答案 0 :(得分:1)

所以使用List

List<Document> pipeline = Arrays.<Document>asList(
    new Document("$project",
        new Document("TOTAL_EMPLOYEE_SALARY",new Document("$sum","$employees.EMP_SALARY"))
    )
);

AggregationOutput output = coll.aggregate(pipeline);