0

well below is my implementation

   db.services_performed.aggregate([{$match:{status:{$in: 
   ["INVOICING","outbound","accepted","PENDING APPROVAL"]}}},{$group:{_id:{date:{$dateToString: 
   {format:"%Y-%m-%d",date:"$created_on"}}},invoicing:{$sum:{$cond:[{$eq: 
   ["$status","INVOICING"]},1,0]}},outnound:{$sum:{$cond:[{$eq: 
   ["$status","outbound"]},1,0]}},accepted:{$sum:{$cond:[{$eq: 
   ["$status","PENDING APPROVAL"]},1,0]}}}}])

how can i convert my query into reactive springboot code please help out with

1 Answer 1

0

Create agregation query using mongo aggregation api:

private Aggregation aggregationQuery() {
    Criteria statusCriteria = Criteria.where("status").in("INVOICING", "outbound", "accepted", "PENDING APPROVAL");
    MatchOperation matchOperation = Aggregation.match(statusCriteria);

    ProjectionOperation projectionOperation = Aggregation.project()
            .and(DateOperators.DateToString.dateOf("created_on").toString("%Y-%m-%d"))
                .as("date")
            .and(ConditionalOperators.when(Criteria.where("status").is("INVOICING"))
                    .then(1)
                    .otherwise(0))
                .as("invoicing")
            .and(ConditionalOperators.when(Criteria.where("status").is("outbound"))
                    .then(1)
                    .otherwise(0))
                .as("outbound")
            .and(ConditionalOperators.when(Criteria.where("status").is("PENDING APPROVAL"))
                    .then(1)
                    .otherwise(0))
                .as("pendingApproval");

    GroupOperation groupOperation = Aggregation.group("date")
            .sum("invoicing").as("invoicing")
            .sum("outbound").as("outbound")
            .sum("pendingApproval").as("pendingApproval");

    return Aggregation.newAggregation(matchOperation, projectionOperation, groupOperation);
}

Define your result class:

@Data
public class ServicePerformedSummary {
    private String date;
    private int invoicing;
    private int outbound;
    private int pendingApproval;
}

Then autowire ReactiveMongoTemplate which is used to perform queries:

@Autowired
private ReactiveMongoTemplate reactiveMongoTemplate;

And then you method to call mongo template with desired aggregation:

public Flux<ServicePerformedSummary> getSummary() {
    return reactiveMongoTemplate.aggregate(aggregationQuery(), "services_performed", ServicePerformedSummary.class);
}

at least it will give you an idea how to achieve what you want

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.