spring data mongodb group by

前端 未结 1 1929
时光取名叫无心
时光取名叫无心 2020-12-09 23:17

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

Student class:

@Docum         


        
相关标签:
1条回答
  • 2020-12-09 23:34

    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"));
    
    0 讨论(0)
提交回复
热议问题