Let appendLimitAndOffsetIfPresent accept unary operators for adjusting limit/offset values instead of appendModifiedLimitAndOffsetIfPresent. Apply simple type extraction for Slice. Add support for aggregation result streaming.
Extend tests, add author tags, update docs.
See #3543.
Original pull request: #3645.
@ -64,6 +71,12 @@ public class StringBasedAggregation extends AbstractMongoQuery {
@@ -64,6 +71,12 @@ public class StringBasedAggregation extends AbstractMongoQuery {
@ -85,8 +98,9 @@ public class StringBasedAggregation extends AbstractMongoQuery {
@@ -85,8 +98,9 @@ public class StringBasedAggregation extends AbstractMongoQuery {
@ -96,38 +110,43 @@ public class StringBasedAggregation extends AbstractMongoQuery {
@@ -96,38 +110,43 @@ public class StringBasedAggregation extends AbstractMongoQuery {
@ -137,6 +156,17 @@ public class StringBasedAggregation extends AbstractMongoQuery {
@@ -137,6 +156,17 @@ public class StringBasedAggregation extends AbstractMongoQuery {
@ -1269,13 +1270,16 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
@@ -1269,13 +1270,16 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
@ -1290,6 +1294,14 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
@@ -1290,6 +1294,14 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
@ -52,7 +55,7 @@ public class PersonAggregate {
@@ -52,7 +55,7 @@ public class PersonAggregate {
public class SumValue {
private final Long total; <5> <7>
private final Long total; <6> <8>
public SumValue(Long total) {
// ...
@ -65,12 +68,13 @@ public class SumValue {
@@ -65,12 +68,13 @@ public class SumValue {
<2> If `Sort` argument is present, `$sort` is appended after the declared pipeline stages so that it only affects the order of the final results after having passed all other aggregation stages.
Therefore, the `Sort` properties are mapped against the methods return type `PersonAggregate` which turns `Sort.by("lastname")` into `{ $sort : { '_id', 1 } }` because `PersonAggregate.lastname` is annotated with `@Id`.
<3> Replaces `?0` with the given value for `property` for a dynamic aggregation pipeline.
<4> `$skip`, `$limit` and `$sort` can be passed on via a `Pageable` argument. Same as in <2>, the operators are appended to the pipeline definition.
<5> Map the result of an aggregation returning a single `Document` to an instance of a desired `SumValue` target type.
<6> Aggregations resulting in single document holding just an accumulation result like eg. `$sum` can be extracted directly from the result `Document`.
<4> `$skip`, `$limit` and `$sort` can be passed on via a `Pageable` argument. Same as in <2>, the operators are appended to the pipeline definition. Methods accepting `Pageable` can return `Slice` for easier pagination.
<5> Aggregation methods can return `Stream` to consume results directly from an underlying cursor. Make sure to close the stream after consuming it to release the server-side cursor by either calling `close()` or through `try-with-resources`.
<6> Map the result of an aggregation returning a single `Document` to an instance of a desired `SumValue` target type.
<7> Aggregations resulting in single document holding just an accumulation result like eg. `$sum` can be extracted directly from the result `Document`.
To gain more control, you might consider `AggregationResult` as method return type as shown in <7>.
<7> Obtain the raw `AggregationResults` mapped to the generic target wrapper type `SumValue` or `org.bson.Document`.
<8> Like in <6>, a single value can be directly obtained from multiple result ``Document``s.
<8> Obtain the raw `AggregationResults` mapped to the generic target wrapper type `SumValue` or `org.bson.Document`.
<9> Like in <6>, a single value can be directly obtained from multiple result ``Document``s.
====
In some scenarios, aggregations might require additional options, such as a maximum run time, additional log comments, or the permission to temporarily write data to disk.
@ -115,5 +119,5 @@ Simple-type single-result inspects the returned `Document` and checks for the fo
@@ -115,5 +119,5 @@ Simple-type single-result inspects the returned `Document` and checks for the fo
. Throw an exception if none of the above is applicable.
====
WARNING: The `Page` return type is not supported for repository methods using `@Aggregation`. However you can use a
`Pageable` argument to add `$skip`, `$limit` and `$sort` to the pipeline.
WARNING: The `Page` return type is not supported for repository methods using `@Aggregation`. However, you can use a
`Pageable` argument to add `$skip`, `$limit` and `$sort` to the pipeline and let the method return `Slice`.