解决 ArrayOperators.in Arrays.asList(value,field)顺序颠倒 导致生成sql无法执行
public class MyIn implements AggregationExpression {
private final Object value;
private MyIn(List<Object> values) {
this.value = values;
}
public static MyIn Buider(final String fieldReference, Object value) {
Assert.notNull(fieldReference, "FieldReference must not be null!");
return new MyIn(Arrays.asList(fieldReference, value));
}
@Override
public Document toDocument(AggregationOperationContext context) {
return this.toDocument(value, context);
}
protected String getMongoMethod() {
return "$in";
}
public Document toDocument(Object value, AggregationOperationContext context) {
return new Document(getMongoMethod(), unpack(value, context));
}
private Object unpack(Object value, AggregationOperationContext context) {
if (value instanceof AggregationExpression) {
return ((AggregationExpression) value).toDocument(context);
}
if (value instanceof List) {
List<Object> sourceList = (List<Object>) value;
List<Object> mappedList = new ArrayList<>(sourceList.size());
sourceList.stream().map((item) -> unpack(item, context)).forEach(mappedList::add);
return mappedList;
}
if (value instanceof Map) {
Document targetDocument = new Document();
Map<String, Object> sourceMap = (Map<String, Object>) value;
sourceMap.forEach((k, v) -> targetDocument.append(k, unpack(v, context)));
return targetDocument;
}
return value;
}
}
.filter("item", MyIn.Buider("$$item.partDesc", parts))