dev777
dev777

Reputation: 1019

How to convert mongo shell code to java code using document and mongo aggregation

How can I convert this code to java? How to use aggregation framework in java ? Can anyone please help me out...

My java code:

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

My mongo shell code :

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

Upvotes: 0

Views: 866

Answers (1)

Neil Lunn
Neil Lunn

Reputation: 151072

So use a 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);

Upvotes: 1

Related Questions