Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring data mongodb group by

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?

like image 957
developer Avatar asked Mar 15 '17 15:03

developer


People also ask

How do I join two MongoDB collections in spring boot?

For performing MongoDB Join two collections, you must use the $lookup operator. It is defined as a stage that executes a left outer join with another collection and aids in filtering data from joined documents. For example, if a user requires all grades from all students, then the below query can be written: Students.

What is TypedAggregation?

A TypedAggregation is a special Aggregation that holds information of the input aggregation type.

What is $$ root?

$$ROOT. The $$ROOT variable contains the source documents for the group. If you'd like to just pass them through unmodified, you can do this by $pushing $$ROOT into the output from the group.

What is Projectionoperation in MongoDB?

Encapsulates the aggregation framework $project -operation. Projection of field to be used in an Aggregation . A projection is similar to a Field inclusion/exclusion but more powerful. It can generate new fields, change values of given field etc.


1 Answers

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"));
like image 149
s7vr Avatar answered Oct 18 '22 10:10

s7vr