Spring mongo 嵌入文档中的切片数组

Spring mongo slice array in embedded document

使用 spring mongo 模板时,嵌入式文档中数组聚合中的 slice 方法对我不起作用。

示例:

收发票:

{
  "reference_number": "aaa111",
  "historical_data": {
    "year": 2010,
    "data": [
      {
        "item_name": "Apple",
        "price": 50
      },
      {
        "item_name": "Book",
        "price": 200
      }
    ]
  }
}

使用mongo模板我只想获取切片中的历史数据。

对于直接出现在根下的需要切片的数组,我找到了使用聚合的解决方案 参考:Spring mongo repository slice

对嵌入文档中的数组应用类似查询returns即使有数据也是一个空列表。

我尝试的查询是:

TypedAggregation<Invoice> agg = newAggregation(Invoice.class,
                match(where("reference_number").is(referenceNumber)),
                project.andExpression("historicalData.data").slice(limit, offset));
        AggregationResults<Invoice> result = mongoTemplate.aggregate(agg, Invoice.class, Invoice.class);

但是这个returns一个空列表数据。

是否有任何其他替代方法来获取嵌入式文档中数组的切片结果?

Invoice.java

@Data
@Document(collection = "invoice")
public class Invoice {

    @Id
    private String id;

    @NotEmpty
    @Indexed(unique = true)
    @Field("reference_number")
    private String referenceNumber = UUID.randomUUID().toString();

   @Valid
    @Field("historical_data")
    private HistoricalData historicalData = new HistoricalData();
}

历史数据:

@Data
public class HistoricalData {
    @NotEmpty
    @Field("year")
    private Intger year;
    @Valid
    @NotNull
    @Field("data")
    private List<InvoiceData> data = new LinkedList<>();
}

注: 我也试过了:

TypedAggregation<Invoice> agg = newAggregation(Invoice.class,
                match(where("reference_number").is(referenceNumber)),
                project.andExpression("historical_data.data").slice(limit, offset));
        AggregationResults<Invoice> result = mongoTemplate.aggregate(agg, Invoice.class, Invoice.class);

但这给了我一个 PropertyPathException。

提前致谢!!

经过几周的努力,我找到了解决方案:

ProjectionOperation project = project().and("historicalRevisionData.data").slice(limit, offset).as("historical_revision_data.data")
                .andInclude("id")
                .and("referenceNumber").as("reference_number");
TypedAggregation<Invoice> agg = newAggregation(Invoice.class,
                match(where("reference_number").is(referenceNumber)),
                project);
AggregationResults<TaxInvoice> aggregate = mongoTemplate.aggregate(agg, Invoice.class, Invoice.class);

希望这对其他人也有帮助。