У меня ниже 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.
Я пробовал указанные ниже варианты, но не работал.
options1.
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);
Также я не знаю, как подобрать группу по 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;}