问题描述
我正在尝试转换以下Mongo查询以用于Spring数据。
db.product.aggregate([
{$unwind: '$barcodes'},{$project: {
_id: 0,productId: '$_id',productTitle: '$title',productvariation: '$variation',barcode: '$barcodes'
}}])
这是我到目前为止一直在尝试的方法。它返回聚合,但具有空值:
UnwindOperation unwindOperation = Aggregation.unwind("barcodes");
Projectionoperation projectStage = Aggregation.project().and("productId").as("_id").and("productTitle")
.as("title")
.and("productvariation").as("variation")
.and("barcodeTitle").as("barcodes.title")
.and("barcodeValue").as("barcodes.value")
.and("barcodeType").as("barcodes.type")
.and("codeStandard").as("barcodes.codeStandard")
.and("quantity").as("barcodes.quantity")
.and("status").as("barcodes.status");
SortOperation sortOperation = Aggregation.sort(Sort.by(Sort.Direction.DESC,"title"));
Aggregation agg = Aggregation.newAggregation(unwindOperation,projectStage,sortOperation);
AggregationResults<BarcodeAggregateList> results = mongoTemplate.aggregate(agg,"product",BarcodeAggregateList.class);
返回的内容:
我要映射到的类(具有getters / setters):
public class BarcodeAggregateList {
private String productId;
private String productTitle;
private String productvariation;
private String barcodeTitle;
private String barcodeValue;
private String barcodeType;
private String codeStandard;
private int quantity;
private String status;
}
数据来自的产品类别:
public class Product implements Serializable {
private static final long serialVersionUID = -998149317494604215L;
private String id;
private String title;
private String description;
private String SKU;
private double cost;
private double retailPrice;
private String status;
private LocalDate launchdate;
private LocalDate discontinueDate;
private String discontinueReason;
private String salesChannel;
private List<Barcode> barcodes;
private Productvariation variation;
private List<supplier> supplier;
private Product parentProduct;
private boolean updateChildren;
private Label label;
private int secondaryStockLevel;
private int primaryStockLevel;
private Date createdDate;
private Date modifiedDate;
private List<Dimension> dimensions;
private boolean isDeleted = false;
}
条形码类
public class Barcode {
private String type;
private String title;
private String value;
private String status;
private String codeStandard;
private int quantity;
}
感谢您对此提供的帮助或资源,以帮助我更好地了解如何执行这些类型的转化。
对于尝试解决类似问题的任何人,我发现以下资源有所帮助:
- https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo.query
- https://xpadro.com/2016/04/data-aggregation-with-spring-data-mongodb-and-spring-boot.html
- https://www.tutorialspoint.com/get-fields-from-multiple-sub-documents-that-match-a-condition-in-mongodb
解决方法
/// https://stackoverflow.com/a/58880555/4233401
type Identical<T,TTest,TTrue,TFalse> = (<U extends T>(
o: U
) => void) extends <U extends TTest>(o: U) => void
? TTrue
: TFalse;
type KeyWithValueOfType<THost,TValueType> = {
[K in keyof THost]: Identical<THost[K],TValueType,K,never>;
// [K in keyof THost]: THost[K] extends TValueType ? K : never;
}[keyof THost];
function genericFunction<T>(
obj: T,methodName: KeyWithValueOfType<T,() => void>
): void {
const method = obj[methodName];
// How can I guarantee that method is callable?
// Or that it is the type that I restricted at KeyWithValueOfType <THost,TValueType>
method(); // This expression is not callable.
// Type 'unknown' has no call signatures.
}
type TTypeMethod = KeyWithValueOfType<IType,() => void>; // = "function" | "method"
genericFunction({} as IType,'f'); // ok
genericFunction({} as IType,'m'); // ok
genericFunction({} as IType,'p'); // ok
类字段为BarcodeAggregateList
,因为null
的and()和as()方法中存在较小的问题。正确的语法是
ProjectionOperation
您写了Aggregation.project().and(SOURCE_FIELD).as(TARGET_FIELD)
,这是错误的
您需要将其写为and("productId").as("_id")
,因为源字段是and("_id").as("productId")
完整代码:
_id