Skip to content

Issue referencing or using field value set with SetWindowFieldsOperation shift #4745

Closed
@alex-ionescu-teamextension

Description

Hello,

I am using spring-data-mongodb-4.2.5.jar and I have the usecase where I am doing a shift to get the next or previous value of a timestamp field using $setWindowFields.
The problem with the Java approach is that the resulting field is not correctly referenced in the aggregation pipeline, resulting in a null value when doing a $dateDiff.

This is how I am doing the $setWindowFields / $dateDiff:

SetWindowFieldsOperation timestampWindowFields = SetWindowFieldsOperation.builder()
                .partitionByField("metaData.deviceId")
                .sortBy(Sort.by(Sort.Direction.ASC, "timestamp"))
                .output(DocumentOperators.valueOf("timestamp").shift(-1).defaultTo(-1))
                .as("previous")
                .build();

DateOperators.DateDiff dateDiff = DateOperators.zonedDateOf("timestamp", DateOperators.Timezone.valueOf("America/Chicago"))
                .diffValueOf("previous", DateOperators.TemporalUnit.from(ChronoUnit.SECONDS));
        SetOperation dateDiffOperation = set("timeDifference").toValue(dateDiff);

And this is how the operator ends up in the aggregation pipeline output:
image

Any ideea why the "endDate" field is referenced like this, is there a way to overcome this issue to get the correct result? How could I do a dateDiff between computed / shifted fields?

Thanks in advance,
Alex

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions