0

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"
        }
    }}
])
2
  • 1
    Could you add the error to question? Commented Apr 22, 2016 at 7:52
  • Actually eclipse is not allowing me to write this line:AggregationOutput output = coll.aggregate(update); .It is asking for cast argument 'update' to List<? extends BSON> Commented Apr 22, 2016 at 8:26

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.