是否有可能在spring-data-mongodb中拥有枢轴?

时间:2015-09-30 15:13:49

标签: mongodb spring-data spring-data-mongodb

我是mongodb和spring数据的新手,并尝试使用mongodb,可以使用spring数据进行pivot in mongodb所述的数据透视操作。我有这种形式的数据:

 Name| SumPrice| type|year 
 A|    50.0|     Retail|2015
 B|    467.0|    Online|2015
 A|    1456.0|   Direct|2015
 B|    1149.53|  Direct|2015
 C|     273.20|  Online|2014
 C|     110.0|   Direct|2014

为此,我创建了一个java类:

    class SampleReport{

     private String name;
     private Double sumUnitPrice;
     private String type;
     private Integer year;
      } 

我想转动w.r.t.输入并希望表格的数据:

  Name |Retail| Online | Direct| year
  A|     50.0 |  0.0|    1456.0 |    2015
  B|     0.0|    467.0|  1149.53|  2015
  C|     0.0|    273.20| 110.0|     2014

你在春季数据网站上提到的例子对我来说并不清楚,或者对我的理解太复杂了。请知道如何使用spring-data对上面的例子做同样的事情。任何答案

此致

克里斯

1 个答案:

答案 0 :(得分:1)

正如您所链接的那样,答案已经存在。

使用弹簧你会像:

 class ZipInfo {
    String id;
    String city;
    String state;
    @Field("pop") int population;
    @Field("loc") double[] location;
 }

 class City {
   String name;
   int population;
 }

class ZipInfoStats {
    String id;
    String state;
    City biggestCity;
    City smallestCity;
 }

TypedAggregation<ZipInfo> aggregation = newAggregation(ZipInfo.class,
    group("state", "city")
        .sum("population").as("pop"),
    sort(ASC, "pop", "state", "city"),
    group("state")
       .last("city").as("biggestCity")
       .last("pop").as("biggestPop")
       .first("city").as("smallestCity")
       .first("pop").as("smallestPop"),
    project()
       .and("state").previousOperation()
       .and("biggestCity")
       .nested(bind("name", "biggestCity").and("population", "biggestPop"))
       .and("smallestCity")
       .nested(bind("name", "smallestCity").and("population",  "smallestPop")),
    sort(ASC, "state")
 );

请记住,聚合的输出将绑定在 ZipInfo.class 中。

弹簧mongodb API和文档的reference链接。

相关问题