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

如何解决如何将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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?