[JAVA-5080] Add chained wrappers for aggregation stages Created: 17/Jul/23  Updated: 03/Oct/23

Status: Backlog
Project: Java Driver
Component/s: Builders
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Unknown
Reporter: Maxim Katcharov Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to JAVA-5081 Support schema classes Backlog
Quarter: FY25Q1
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

In JAVA-3879, we implemented aggregation expressions using the chaining pattern. This pattern differs from the implementation of the the existing aggregation stages, which do not use a chaining approach.

Creating chained implementations of the aggregation stages would a) make the API as a whole more consistent, b) remove the need for "glue" like `current()`, and c) would allow users to take advantage of the "debug" API for development. The negative effect is that this would introduce a second API, which we would need to commit to.

An example of the API:

List<Diagram> results = diagramsCollectionObject
        .debug()
        .addField("area", d -> d.shapes
                .map(shape -> shape.w.multiply(shape.h))
                .sum(v -> v))
        .filter(d -> d.area.gt(of(10)))
        .stage(Aggregates.count())
        .toList(); 

This performs an aggregation on a collection of diagrams, with shapes. It adds a field to the diagram, filters on that field, and then counts the results.

I used a "toList" method in the POC for convenience. The "debug" method enables stage-level debugging, via $documents, of the aggregation chain. The "stage" method accepts the existing BSON stages. For example, "addField" is implemented as:

return stage(Aggregates.addFields(new Field<>(fieldName, in.apply(getCurrent())))); 

This removes the need for "current()", since in the example, current is "d", just as each shape item under "map" is represented as "shape". The above example also uses schema classes (JAVA-5081) to save on "getField" calls.

 


Generated at Thu Feb 08 09:03:41 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.