Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am using spring data Mongodb in my project and refer the below classes for my query on grouping the results:

Student class:

@Document(collection = "student")
public class Student {

    @Id
    private String id;

    private String firstName;

    private String lastName;

    //other fields

    //getters & setters

}

StudentResults (dto):

public class StudentResults {

    private String firstName;

    private List<String> studentIds; //I need List<Student> here

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public List<String> getStudentIds() {
        return studentIds;
    }

    public void setStudentIds(List<String> studentIds) {
        this.studentIds = studentIds;
    }
}

StudentServiceImpl class:

public class StudentServiceImpl implements StudentService {
    @Autowired
    private MongoTemplate mongoTemplate;

    public List<StudentResults> findStudentsGroupByFirstName() {
        TypedAggregation<Student> studentAggregation = 
               Aggregation.newAggregation(Student.class,
               Aggregation.group("firstName").
               addToSet("id").as("studentIds"),
               Aggregation.project("studentIds").
               and("firstName").previousOperation());

        AggregationResults<StudentResults> results = mongoTemplate.
             aggregate(studentAggregation, StudentResults.class);

        List<StudentResults> studentResultsList = results.getMappedResults();

        return studentResultsList;
    }
}

Using the above code, I am able to retrieve the List<String> studentIds successfully, but I need to retrieve List<Student> students using Aggregation.group()? Can you help?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
108 views
Welcome To Ask or Share your Answers For Others

1 Answer

Change your TypedAggregation part to below and add students field to StudentResults

 TypedAggregation<Student> studentAggregation = Aggregation.newAggregation(Student.class,
               Aggregation.group("firstName").
               push("$$ROOT").as("students"));

$$ROOT will push the whole document.

Update:

TypedAggregation<Student> studentAggregation = Aggregation.newAggregation(Student.class,
              Aggregation.group("firstName").
                 push(new BasicDBObject
                       ("_id", "$_id").append
                       ("firstName", "$firstName").append
                       ("lastName", "$lastName")).as("students"));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...