2016-04-22 35 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);