有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在Spring数据mongo查询中使用聚合和排序

我有下面的SQL查询

 List<EmployeeInfo> employeesInfoList = Lists.newArrayList();
        final StringBuilder query = new StringBuilder(265);
        query.append("employeeKey.code = ? AND ");
        query.append("employeeKey.month = ? AND ");
        query.append("payProjection.details[*]( month = ? AND netValue != ? ) AND ");
        query.append("status.active = ? AND ");
        query.append("status.type = ? ");
        query.append("GROUP BY employeeKey.employeeNumber ");
        query.append("ORDER BY employeeKey.employeeNumber ");
        final SQLQuery<EmployeeInfo> sqlQuery;    
        sqlQuery = getSQLQuery(query.toString(), code, month, month, 0, true, type);    
        sqlQuery.setProjections("employeeKey", "status", "payProjection");

上述查询必须转换为Spring数据mongo查询

我尝试了以下选项,但不起作用

选择1

final Criteria criteria = where(employeeKey.code).is(code)
            .andOperator(where(employeeKey.month).is(month),
                where("payProjection.details.month").is(month),
                where("payProjection.details.netValue").ne(0),
                where(status.active).in(true), where(status.type).in(type));
        final Query query = new Query(criteria);
        query.fields().include(employeeKey).include(status).include("payProjection");
        query.with(new Sort(Sort.Direction.ASC, "employeeKey.employeeNumber "));
        return mongoTemplate.find(query, EmployeeInfo.class);

我也不知道如何根据员工的关键来适应这个群体。上述mongo查询中的employeenumber。帮我解决这个问题

选择2

 final AggregationOperation match = Aggregation.match( where(employeeKey.code).is(code)
            .andOperator(where(employeeKey.month).is(month),
                where("payProjection.details.month").is(month),
                where("payProjection.details.netValue").ne(0),
                where(status.active).in(true), where(status.type).in(type)););
        final AggregationOperation group = Aggregation.group("employeeKey.employeeNumber");
        final AggregationOperation sort = Aggregation.sort(Sort.Direction.ASC, "employeeKey.employeeNumber");
        final Aggregation aggregation = Aggregation.newAggregation(Aggregation.unwind("employeeKey.employeeNumber"), match, group, sort);
        return mongoTemplate.aggregate(aggregation, EmployeeInfo.class, EmployeeInfo.class).getMappedResults();

我的对象结构:

public class EmployeeInfo implements Serializable {

    private static final long serialVersionUID = 1060791921216076800L;

    @Id
    private EmployeeKey employeeKey;

    private Status status;

    private PayProjection payProjection;   

}
EmployeeKey{
 private String code;    
    private int employeeNumber;
    private String month;
    }

    Status{
     private boolean active;
      private Type type;
      }

      PayProjection{
      List<Details> details
      }

      Details{
    private String month;
    private int netValue;}  

共 (0) 个答案