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

Mongodb 嵌入式文档中的Spring mongo切片数组,mongodb,spring-data,spring-data-mongodb,spring-mongodb,mongotemplate,Mongodb,Spring Data,Spring Data Mongodb,Spring Mongodb,Mongotemplate,嵌入文档中数组aggreation中的slice方法不适用于使用spring mongo模板的我 例如: 发票收款: { "reference_number": "aaa111", "historical_data": { "year": 2010, "data": [ { "item_name": "Apple", "price": 50 }, { "item_name": "Book"

嵌入文档中数组aggreation中的slice方法不适用于使用spring mongo模板的我

例如:

发票收款:

{
  "reference_number": "aaa111",
  "historical_data": {
    "year": 2010,
    "data": [
      {
        "item_name": "Apple",
        "price": 50
      },
      {
        "item_name": "Book",
        "price": 200
      }
    ]
  }
}
@Data
public class HistoricalData {
    @NotEmpty
    @Field("year")
    private Intger year;
    @Valid
    @NotNull
    @Field("data")
    private List<InvoiceData> data = new LinkedList<>();
}
使用mongoTemplate,我希望只获取切片中的历史数据

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

对嵌入文档中的数组应用类似的查询,即使有数据,也会返回空列表

我尝试的查询是:

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);
历史数据:

{
  "reference_number": "aaa111",
  "historical_data": {
    "year": 2010,
    "data": [
      {
        "item_name": "Apple",
        "price": 50
      },
      {
        "item_name": "Book",
        "price": 200
      }
    ]
  }
}
@Data
public class HistoricalData {
    @NotEmpty
    @Field("year")
    private Intger year;
    @Valid
    @NotNull
    @Field("data")
    private List<InvoiceData> data = new LinkedList<>();
}
@数据
公共类历史数据{
@空空如也
@字段(“年”)
私家车年检;
@有效的
@NotNull
@字段(“数据”)
私有列表数据=新的LinkedList();
}

注意:我也尝试过:

typedaggeration agg=newAggregation(Invoice.class、,
匹配(其中(“参考编号”)为(参考编号)),
project.andExpression(“历史数据”).slice(限制、偏移);
AggregationResults=mongoTemplate.aggregate(agg,Invoice.class,Invoice.class);
但这给了我一个PropertyPathexException


提前谢谢

经过一周的努力,我找到了解决办法:

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);
projectOnOperation project=project().和(“historicalRevisionData.data”).slice(限制、偏移量).as(“historicalRevisionData.data”)
.和包括(“id”)
和(“参考编号”)。作为(“参考编号”);
TypedAggregation agg=newAggregation(Invoice.class,
匹配(其中(“参考编号”)为(参考编号)),
项目);
AggregationResults aggregate=mongoTemplate.aggregate(agg,Invoice.class,Invoice.class);

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

您能在这里指导我吗:?