1

if I have a collection of books :-

{author: "tolstoy", title:"war & peace", price:100, pages:800}
{author: "tolstoy", title:"Ivan Ilyich", price:50,  pages:100}

and if i want a result like this after grouping them by author :-

{ author: "tolstoy",
   books: [
      {author: "tolstoy", title:"war & peace", price:100, pages:800}
      {author: "tolstoy", title:"Ivan Ilyich", price:50,  pages:100}
]
}

using raw mongo queries I can do something like this:-

{$group: {
 _id: "$author",
 books:{$push: {author:"$author", title:"$title", price:"$price", pages:"$pages"}},
}}

But how do I do this using spring , I tried something like this:-

 private GroupOperation getGroupOperation() {
    return group("author").push("title").as("title").push("price").as("price").push("pages").as("pages");
}

but this does not seem to work. Any help would be appreciated.

UPDATE:- I used the solution as in the link suggested by @Veeram and it works great but I ran into another issue when I project it. I have my projection class which looks like:-

public class BookSummary{

private String author;

private List<Book> bookList;
  //all getters and setters below
}    

The group method looks like this:-

 private GroupOperation getGroupOperation() {
    return group("author").push(new BasicDBObject("id","$_id").append("title","$title").append("pages","$pages").append("price","$price")).as("bookList");
}

the projection method looks like this:-

private ProjectionOperation getProjectOperation() {
    return project("author").and("bookList").as("bookList");
}

and the final aggregation operation:-

mongoTemplate.aggregate(Aggregation.newAggregation(groupOperation,projectionOperation), Book.class, BookSummary.class).getMappedResults();

However this gives the result:-

[
{
    "author": null,
    "bookList": [
        {
            "id": null,
            "title": "title1",
            "pages": "100",
            "price":"some price"
        },
        {
            "id": null,
            "title": "title2",
            "pages": "200",
            "price":"some price"
        }
    ]
}
]

Why is the author and id null here? Any help would be appreciated

Rasmus
  • 8,248
  • 12
  • 48
  • 72
  • 1
    Possible duplicate of [MongoDB $aggregate $push in Java Spring Data](https://stackoverflow.com/questions/39393672/mongodb-aggregate-push-in-java-spring-data) – s7vr Jan 04 '18 at 18:59
  • Thanks @Veeram +1 . That definitely helped. Have updated the question . Could you please take a look – Rasmus Jan 04 '18 at 19:45
  • 1
    Take a look at https://stackoverflow.com/q/42814325/2683814 for pushing **all** fields – s7vr Jan 04 '18 at 19:45
  • while that got all the fields , the author field by which the group is happening is still null @Veeram – Rasmus Jan 04 '18 at 19:49
  • is there something wrong in the projection that I am doing? – Rasmus Jan 04 '18 at 19:49
  • 1
    Yes, the projection should be `project("bookList").and("author", "_id")` – s7vr Jan 04 '18 at 19:50
  • 1
    worked great! wish all this could be accepted as an answer. Thanks much! – Rasmus Jan 04 '18 at 19:55

1 Answers1

0

You should be projecting using _id instead in the project phase.

private ProjectionOperation getProjectOperation() {
    return project("_id").and("bookList").as("bookList");
}