Here is a solution using the MongoRepository API (originally you are looking for):
Repository with the aggregation method:
public interface SampleRepository extends MongoRepository<Sample, Integer> {
public static final String match1 = "{ '$match':{ 'pdg': ?0, 'type': ?1, 'date':{ '$gte': ?2, '$lte': ?3 } } }";
public static final String sort = "{ '$sort':{ 'pdg':1, 'date':1 } }";
public static final String group = "{ '$group':{ '_id':{ 'site':'$site' }, 'wf':{ '$push':{ 'date':'$date', 'resources': '$resources', 'site':'$site' } }, 'tresources':{ '$sum':'$resources' } } }";
public static final String match2 = "{ '$match':{ 'tresources':{ '$gt':0 } } }";
@Aggregation(pipeline = { match1, sort, group, match2 })
List<PojoOut> aggregateBySample(String pdg, String type, String date1, String date2);
}
Running the aggregation:
List<PojoOut> list = sampleRepository.aggregateBySample("PD", "type1", "2019-01-01", "2019-12-01");
PojoOut class is of the output type of this document:
{
"_id" : {
"site" : "T"
},
"wf" : [
{
"site" : "T",
"date" : "2019-05-01",
"resources" : 2
},
{
"site" : "T",
"date" : "2019-11-01",
"resources" : 1
}
],
"tresources" : 3,
"_class" : "com.example.demo.PojoOut"
}