MongoDb aggregation query conversion to Spring-data -


i have aggregate function wherein structure of output of method :

 {     "_id" : {         "vehiclenumber" : "hr55w8395",         "vehicletype" : "type_32"     },     "mileage" : [         3.4200838876537736,         3.6082731400212595,         3.7118590539249254,         2.9805899622661784,         5.227747018794297,         3.222515049264743,         3.8845896154778603,         3.548054585322907,         3.010341324091653     ] } 

the aggregate function

db.hop.aggregate([{$group : {_id : {vehiclenumber : "$vehiclenumber", vehicletype : "$vehicletype"}, mileage: {$push : "$mileage"}}}]) 

and aggregate in spring written :

aggregationoperation match = match(criteria.where("starthubouttime").gte(startdatetime).and("endhubintime").lte(enddatetime).and("vehiclenumber").exists(true).and("mileage").exists(true));         //groupoperationbuilder group = aggregation.group("vehiclenumber","vehicletype").push("mileage");         aggregationoperation graggregationoperation =aggregation.group("vehiclenumber","vehicletype").push(new basicdbobject("mileage","$mileage")).as("mileage"); ;//group.as("_id");         aggregationoperation project = aggregation.project("mileage").andinclude("vehiclenumber","vehicletype");         aggregation newaggregation = aggregation.newaggregation(match, graggregationoperation);         aggregationresults<aggregatedmileageoutput> aggregatedmileageoutputs = mongotemplate.aggregate(newaggregation, hop.class, aggregatedmileageoutput.class); 

and output : aggregatedmileageoutput

class aggregatedmileageoutput {     private string vehiclenumber;     private vehicletype vehicletype;     private arraylist<double> mileage; } 

but during aggregation operation getting error :

request processing failed; nested exception org.springframework.data.mapping.model.mappingexception: no mapping metadata found java.lang.double  stack trace :  org.springframework.data.mapping.model.mappingexception: no mapping metadata found java.lang.double     org.springframework.data.mongodb.core.convert.mappingmongoconverter.read(mappingmongoconverter.java:228)     org.springframework.data.mongodb.core.convert.mappingmongoconverter.readcollectionorarray(mappingmongoconverter.java:906)     org.springframework.data.mongodb.core.convert.mappingmongoconverter.readvalue(mappingmongoconverter.java:1183)     org.springframework.data.mongodb.core.convert.mappingmongoconverter.access$200(mappingmongoconverter.java:78)     org.springframework.data.mongodb.core.convert.mappingmongoconverter$mongodbpropertyvalueprovider.getpropertyvalue(mappingmongoconverter.java:1133)     org.springframework.data.mongodb.core.convert.mappingmongoconverter.getvalueinternal(mappingmongoconverter.java:869)     org.springframework.data.mongodb.core.convert.mappingmongoconverter$1.dowithpersistentproperty(mappingmongoconverter.java:282)     org.springframework.data.mongodb.core.convert.mappingmongoconverter$1.dowithpersistentproperty(mappingmongoconverter.java:270)     org.springframework.data.mapping.model.basicpersistententity.dowithproperties(basicpersistententity.java:309)     org.springframework.data.mongodb.core.convert.mappingmongoconverter.read(mappingmongoconverter.java:270)     org.springframework.data.mongodb.core.convert.mappingmongoconverter.read(mappingmongoconverter.java:231)     org.springframework.data.mongodb.core.convert.mappingmongoconverter.read(mappingmongoconverter.java:191)     org.springframework.data.mongodb.core.convert.mappingmongoconverter.read(mappingmongoconverter.java:187)     org.springframework.data.mongodb.core.convert.mappingmongoconverter.read(mappingmongoconverter.java:78)     org.springframework.data.mongodb.core.mongotemplate$readdbobjectcallback.dowith(mongotemplate.java:2215)     org.springframework.data.mongodb.core.mongotemplate$unwrapandreaddbobjectcallback.dowith(mongotemplate.java:2248)     org.springframework.data.mongodb.core.mongotemplate.returnpotentiallymappedresults(mongotemplate.java:1533)     org.springframework.data.mongodb.core.mongotemplate.aggregate(mongotemplate.java:1509)     org.springframework.data.mongodb.core.mongotemplate.aggregate(mongotemplate.java:1435) 

please this.

the error telling 1 of variable names in pipeline not same (spelling, upper/lower case) names of fields in mongodb collection. try re-writing aggregation operation as

aggregation agg = newaggregation(     match(         criteria.where("starthubouttime").gte(startdatetime)             .and("endhubintime").lte(enddatetime)             .and("vehiclenumber").exists(true)             .and("mileage").exists(true)     ),     group("vehiclenumber", "vehicletype").push("mileage").as("mileage")  );  aggregationresults<aggregatedmileageoutput> results = mongotemplate.aggregate(agg,  hop.class, aggregatedmileageoutput.class);  list<aggregatedmileageoutput> mappedresults = results.getmappedresults();  aggregatedmileageoutput firstitem = mappedresults.get(0); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -