java - Spring-Data not summing up the quarterly results -
i new mongodb , spring-data, , referred stackoverflow link grouping quarterly wise , link using $cond operator in spring data , framed code snippet below, retrieving quarterwise sales report in mongodb:
string pipeline = "{$project:{_id:1,'unitssold':1,'datesold':1,'results': 1 , 'productname': 1, 'year':{$year:['$datesold']}, "+ "'quarter':{$cond:[{$lte:[{$month:'$datesold'},3]},"+ "'first'," + "{$cond:[{$lte:[{$month:'$datesold'},6]},"+ "'second',"+ "{$cond:[{$lte[{$month:'$datesold'},9]},"+"'third',"+ "'fourth']}]}]}}},"+ "{$group:{'_id':{ 'year':'$year', 'quarter':'$quarter'}, 'unitssold': { $sum: '$unitssold' },'results':{$push:'$$root'}}}"; dbobject operation = (dbobject)json.parse (pipeline); typedaggregation<samplereport> aggregation =newaggregation(samplereport.class, new dbobjectaggregationoperation(operation) ); aggregationresults<samplereport> result =mongotemplate.aggregate(aggregation, samplereport.class); list<samplereport> list = result.getmappedresults(); for(samplereport r : list) { system.out.println (r.getproductname() + " : " + r.getunitssold() + " : " + r.getquarter() +":: "+r.getyear()); }
the problem not summing units sold. please lemme know going wrong spring data. query gets required results using robomongo.
regards
kris
if works in client got lost in transation. there things can clean here make more simplified.
can suggest more efficient "math" approach determining current quarter rather current nested conditional statements, if nothing else make things lot cleaner. in addition of "efficiency" should not using $project
preceeding $group
makes logical sense combine 1 stage:
[ { "$group": { "_id": { "year": { "$year": "$datesold" }, "quarter": { "$add": [ { "$subtract": [ { "$divide": [{ "$subtract": [{ "$month": "$datesold" },1]},3]}, { "$mod": [ { "$divide": [{ "$subtract": [{ "$month": "$datesold" },1]},3]}, 1 ]} ]}, 1 ] } }, "unitssold": { "$sum": "$unitssold" } }} ]
by means add in "$push": "$$root"
if must, cutting down involved logic , putting single pipeline stage logical largely point here.
the next phase suggest code natively. whilst may tempting think have json notation can use, find in time neither flexible nor provide readability place in long strings , rely on parsing them. going want interpolate local variables @ stage
aggregation aggregation = newaggregation( new customgroupoperation( new basicdbobject("$group", new basicdbobject("_id", new basicdbobject("year",new basicdbobject("$year","$datesold")) .append("quarter",new basicdbobject( "$add",arrays.aslist( new basicdbobject("$subtract",arrays.aslist( new basicdbobject("$divide",arrays.aslist( new basicdbobject("$subtract",arrays.aslist( new basicdbobject("$month","$datesold"), 1 )), 3 )), new basicdbobject("$mod",arrays.aslist( new basicdbobject("$divide", arrays.aslist( new basicdbobject("$subtract",arrays.aslist( new basicdbobject("$month", "$datesold"), 1 )), 3 )), 1 )) )), 1 ) )) ) .append("unitssold", new basicdbobject("$sum", "$unitssold")) ) ) );
you seem have abstracted other code, prefer implement customgroupoperation
in way not going conflict using other spring-mongo aggregation helpers within newaggregation
contruction:
public class customgroupoperation implements aggregationoperation { private dbobject operation; public customgroupoperation (dbobject operation) { this.operation = operation; } @override public dbobject todbobject(aggregationoperationcontext context) { return context.getmappedobject(operation); } }
but stated, if getting 0
result it's field naming or "type" field differently named or in fact string. identical statement should fail in other client in similar way, , remedy fix naming or "type" appropriate.
this "cleaner" approach doing. "math" sound indexed quarter , can adapted other "financial quarters" appropriate via simple mapping. consolidation of pipeline stages here provide significant performance gains in line overall size of data $project
means un-necessary pass through data pre-adjust fields, don't want.
fix definition , execution , check fields , data see correctly named , typed.
Comments
Post a Comment