I am trying to use agrregation using spring mongodb template. The grouping has to be done on third level of document. input document is
{
"_id": "59036b0fa036cc28c8e07db6",
"sections": [{
"srcName": "test1",
"data": [{
"srcKey": "",
"rowIdx": 0,
"values": [{
"srcDesc": "Assets"
},
{
"srcDesc": "NonAssets"
},
{
"srcDesc": "liabilities"
}
]
},
{
"srcKey": "01",
"rowIdx": 1,
"values": [{
"srcDesc": "NonAssets"
}]
}
]
}]
}
Essentially I want to run query like
select distinct(srcdesc) from document where srcName="test1";
Please see that srcDesc is third level of nesting. I am trying below java code
private MatchOperation getMatchOPeration(String sectionName){
Criteria criteira=Criteria.where("sectionName").in(sectionName);
return match(criteira);
}
private GroupOperation getGroupOperaion(){
return group("srcDesc").last("srcDesc").as("srcDesc");
}
private ProjectionOperation getProjectionOPeration(){
return project("srcDesc").and("srcDesc").previousOperation();
}
public List<SourceName> findAllSourceNamesBySection(String sectionName){
List<SectionsDocument> sourceNameList=new ArrayList<>();
MatchOperation matchOPeration=getMatchOPeration(sectionName);
GroupOperation groupOperation=getGroupOperaion();
ProjectionOperation projectionOperation=getProjectionOPeration();
AggregationResults<SectionsDocument> aggregationResults=
mongoTemplate.aggregate(Aggregation.newAggregation(
matchOPeration,
unwind("sections.data.values"),
groupOperation,
projectionOperation),StatDocument.class,SectionsDocument.class);
sourceNameList=aggregationResults.getMappedResults();
return new ArrayList<>();
}