如何将MongoDB查询转换为Spring数据查询

问题描述

我正在尝试转换以下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);

返回的内容

return example

我要映射到的类(具有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://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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...