我有下面的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 DATA 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查询中按employeekey.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;}