Home > database >  Mongo Aggregation. Project () and Aggregation. Group (). The sum of () in the Springboot usage probl
Mongo Aggregation. Project () and Aggregation. Group (). The sum of () in the Springboot usage probl

Time:09-26

Involved in the code is as follows:
 
List Operations=new ArrayList (a);
Operations. The add (
Aggregation. Project (). AndExpression (" substr (startTime, 0, 10) "), the as (" day ")
);
Operations. The add (
Aggregation. Group (" day ")
Sum (" $duration "). The as (" totalDuration ")
);
Operations. The add (
Aggregation. The match (
The Criteria. The where (" : ") is (ccId)
And (" startTime "). Gte (dateFrom). Lte (dateTo)
)
);
Operations. The add (
Aggregation. Sort (sort. Direction. ASC, "_id")//ascending sort by date
);
Aggregation Aggregation=Aggregation. NewAggregation (operations);
AggregationResults Results=mongoTemplate. Aggregate (
Aggregation, "t_call_record", BasicDBObject class);
List MappedResults=the getMappedResults ();

Breakpoint debugging, implementation to
 
AggregationResults Results=mongoTemplate. Aggregate (
Aggregation, "t_call_record", BasicDBObject class);

Code error, tip:
 
Java. Lang. IllegalArgumentException: Invalid reference '$duration!

"Duration" is a collection of a key in the mongo, business requirements is daily statistical duration key corresponding to the value of the cumulative values,
I will Aggregation in another business method. The project () and Aggregation. The group (" day "). The count () as (" totalCnt ") is used to statistical number no error, thought is the same Aggregation rows, the sum () and usage of the count () should be the same, after I made several attempts to find if you remove
 
Operations. The add (
Aggregation. Group (" day ")
Sum (" $duration "). The as (" totalDuration ")
);

This code, the error will not quote, but also do not accord with my business logic,
So I want to ask you a great god, and used in springboot mongoTemplate mongo's interactive operation, related grammar to Aggregation. The project () and Aggregation. The group (). The sum () is used in combination, are there any other restrictions or grammar? Thank you very much!

CodePudding user response:

Under the roof to yourself and don't sink so quickly

CodePudding user response:

O $duration represents a variable
Directly with duration

CodePudding user response:

Operations. The add (
Aggregation. project (" duration ") . AndExpression (" substr (startTime, 0, 10) "), the as (" day ")
);
Operations. The add (
Aggregation. Group (" day ")
. the sum (" duration "). the as (" totalDuration ")
);
  • Related