1

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.

1 Answer 1

2

Replace your $group stage with

Aggregation.group("mkdius").last("mkdius").as("mkdius").push("$$ROOT").as("imgsId"),

$$ROOT will push the whole document.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.