2016-04-22 46 views
0

如何将此代码转换为java?如何在java中使用聚合框架?任何人都可以请帮我...如何使用文档和mongo聚合将mongo shell代码转换为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 

我蒙戈shell代码:

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

您可以加入问题的错误? – Schore

+0

其实eclipse不允许我写这行:AggregationOutput output = coll.aggregate(update); 。它要求列表<'更新'。扩展BSON> – dev777

回答

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);