java - Spring Data equivalent of aggregation query -
i'm having hard time figuring out how write spring data implementation below aggregation query. background of problem can found here
db.asset.aggregate([{$unwind:"$folderids"}, {$group:{_id: "$folderids",assets:{$push: {assets_id:"$_id",display_name:"$displayname"}}}}])
i have got below piece of code far
aggregationoperation unwind = aggregation.unwind("folderids"); aggregationoperation groupfolderids = aggregation.group("folderids") .push(new basicdbobject().put("assetid", "$_id")).as("assets"); aggregation aggregation = aggregation.newaggregation(unwind, groupfolderids); aggregationresults<map> results = mongotemplate.aggregate(aggregation, "asset", map.class);
i know argument .push() has fixed don't know how. extract few fields asset document in result. appreciated. thank you.
if supplying basicdbobject $push
use .append
method add additional fields:
aggregation aggregation = newaggregation( unwind("folderids"), group("folderids") .push( new basicdbobject("assets_id","$_id") .append("display_name","$displayname") ).as("assets") );
also note when using "typed" output, class type use must match structure of "output" , not "input" class being used of course different.
Comments
Post a Comment