I'm trying to group a object based on a query with spring data and mongodb, that's what i do till now:
Mongodb data:
{
"_id" : ObjectId("58af31feef34aa45476d2be9"),
"_class" : "my.model.Image",
"file" : "0006000.jpg",
"number" : "123",
"mkdius" : "Fiscalization 432",
"status" : "UNCOMPLETED",
"createdAt" : ISODate("2017-02-23T16:03:26.612-03:00")
},
{
"_id" : ObjectId("58af31feef34aa45476d2bf3"),
"_class" : "my.model.Image",
"file" : "9781.jpg",
"number" : "987",
"mkdius" : "Fiscalization 432",
"status" : "UNCOMPLETED",
"createdAt" : ISODate("2017-02-23 16:03:26.866-03:00")
},
{
"_id" : ObjectId("58af31feef34aa45476d2bea"),
"_class" : "my.model.Image",
"file" : "00016.jpg",
"number" : "432",
"mkdius" : "Fiscalization 4154",
"status" : "UNCOMPLETED",
"createdAt" : ISODate("2017-02-23T16:03:26.835-03:00")
}
My aggregation function:
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.match(Criteria.where("status").is(Status.UNCOMPLETED)), // Match
Aggregation.group("mkdius").last("mkdius").as("mkdius").addToSet("id").as("imgsId"), // Grouping
Aggregation.project("imgsId").and("cd").previousOperation()); // Projecting
AggregationResults<InitApp.result> groupResults = this.mongoTemplate.aggregate(aggregation, Image.class, InitApp.result.class);
groupResults.getMappedResults().forEach(System.out::println);
Result that i got:
InitApp.result(mkdius=Fiscalization 432, imgIds=[58af31feef34aa45476d2be9,58af31feef34aa45476d2bf3]
InitApp.result(mkdius=Fiscalization 4154, imgIds=[58af31feef34aa45476d2bea]
What i expect:
InitApp.result(mkdius=Fiscalization 432, imgs=[{
"_id" : ObjectId("58af31feef34aa45476d2be9"),
"_class" : "my.model.Image",
"file" : "0006000.jpg",
"number" : "123",
"mkdius" : "Fiscalization 432",
"status" : "UNCOMPLETED",
"createdAt" : ISODate("2017-02-23T16:03:26.612-03:00")
},{
"_id" : ObjectId("58af31feef34aa45476d2bf3"),
"_class" : "my.model.Image",
"file" : "9781.jpg",
"number" : "987",
"mkdius" : "Fiscalization 432",
"status" : "UNCOMPLETED",
"createdAt" : ISODate("2017-02-23 16:03:26.866-03:00")
}]
InitApp.result(cd=Fiscalization 4154, imgs=[{
"_id" : ObjectId("58af31feef34aa45476d2bea"),
"_class" : "my.model.Image",
"file" : "00016.jpg",
"number" : "432",
"mkdius" : "Fiscalization 4154",
"status" : "UNCOMPLETED",
"createdAt" : ISODate("2017-02-23T16:03:26.835-03:00")
}]
I don't know if it's possible to do, aggregate the data and put the current object in the response.
Thanks.