2

The slice method in aggreation for array within an embedded document is not working for me using spring mongo template.

Example:

Invoice collection:

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

Using mongoTemplate I would like to get only the historical data in slices.

For the arrays which needs to be sliced that appear directly under the root I had found a solution using aggregation Refer : Spring mongo repository slice

Applying a similar query for array in an embedded document returns an empty list even if there is data.

The query that I was trying was :

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);

But this returns an empty list of data.

Is there any other alternative way to get the sliced result for arrays within an embedded document?

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();
}

HistoricalData:

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

Note : I have also tried :

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);

But this gave me a PropertyPathException.

Thanks in advance!!

1 Answer 1

5

After a weeks struggle I have figured out a solution for this:

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);

Hoping that this would help someone else too.

Sign up to request clarification or add additional context in comments.

1 Comment

Could you please guide me here: stackoverflow.com/questions/55532310/…?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.