Skip to content

Sort without grouped fields #3917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1053531172 opened this issue Dec 21, 2021 · 2 comments
Closed

Sort without grouped fields #3917

1053531172 opened this issue Dec 21, 2021 · 2 comments
Labels
type: bug A general bug

Comments

@1053531172
Copy link

1053531172 commented Dec 21, 2021

    db.rep_report_param_label.aggregate([{
        "$match": {}
    }, {
        "$group": {
            "_id": {
                "_id:null": "$_id:null",
                "label_name": "$label_name"
            }
        }
    }, {
        "$project": {
            "label_name": "$_id.label_name",
            "_id": 0
        }
    }, {
        "$sort": {
            "serial_number": - 1,
            "label_name": - 1
        }
    }, {
        "$skip": 290
    }, {
        "$limit": 10
    }]) 

this is ok in mongodb.
but i use in spring data mongodb v3.0.0 like this,

    GroupOperation groupOperation = Aggregation.group("_id:null", "label_name");
    aggregationOperations.add(groupOperation);
    countAggregationOperations.add(groupOperation);

    ProjectionOperation projectionOperation = Aggregation.project("label_name").andExclude("_id");
    aggregationOperations.add(projectionOperation);
    countAggregationOperations.add(projectionOperation);

    Sort sort = Sort.by(Sort.Direction.DESC, "serial_number");
    SortOperation sortOperation = new SortOperation(sort).and(Sort.Direction.DESC, "label_name");
    aggregationOperations.add(sortOperation);

it was wrong

    java.lang.IllegalArgumentException: Invalid reference 'serial_number'!
        at org.springframework.data.mongodb.core.aggregation.ExposedFieldsAggregationOperationContext.getReference(ExposedFieldsAggregationOperationContext.java:114) ~[spring-data-mongodb-3.0.0.RELEASE.jar:3.0.0.RELEASE]
        at org.springframework.data.mongodb.core.aggregation.ExposedFieldsAggregationOperationContext.getReference(ExposedFieldsAggregationOperationContext.java:86) ~[spring-data-mongodb-3.0.0.RELEASE.jar:3.0.0.RELEASE]
        at org.springframework.data.mongodb.core.aggregation.InheritingExposedFieldsAggregationOperationContext.resolveExposedField(InheritingExposedFieldsAggregationOperationContext.java:62) ~[spring-data-mongodb-3.0.0.RELEASE.jar:3.0.0.RELEASE]
        at org.springframework.data.mongodb.core.aggregation.ExposedFieldsAggregationOperationContext.getReference(ExposedFieldsAggregationOperationContext.java:109) ~[spring-data-mongodb-3.0.0.RELEASE.jar:3.0.0.RELEASE]
        at org.springframework.data.mongodb.core.aggregation.ExposedFieldsAggregationOperationContext.getReference(ExposedFieldsAggregationOperationContext.java:86) ~[spring-data-mongodb-3.0.0.RELEASE.jar:3.0.0.RELEASE]
        at org.springframework.data.mongodb.core.aggregation.SortOperation.toDocument(SortOperation.java:73) ~[spring-data-mongodb-3.0.0.RELEASE.jar:3.0.0.RELEASE]
        at org.springframework.data.mongodb.core.aggregation.AggregationOperation.toPipelineStages(AggregationOperation.java:55) ~[spring-data-mongodb-3.0.0.RELEASE.jar:3.0.0.RELEASE]
        at org.springframework.data.mongodb.core.aggregation.AggregationOperationRenderer.toDocument(AggregationOperationRenderer.java:56) ~[spring-data-mongodb-3.0.0.RELEASE.jar:3.0.0.RELEASE]
        at org.springframework.data.mongodb.core.aggregation.Aggregation.toPipeline(Aggregation.java:721) ~[spring-data-mongodb-3.0.0.RELEASE.jar:3.0.0.RELEASE]
        at org.springframework.data.mongodb.core.AggregationUtil.createPipeline(AggregationUtil.java:95) ~[spring-data-mongodb-3.0.0.RELEASE.jar:3.0.0.RELEASE]
        at org.springframework.data.mongodb.core.MongoTemplate.doAggregate(MongoTemplate.java:2118) ~[spring-data-mongodb-3.0.0.RELEASE.jar:3.0.0.RELEASE]
        at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:2093) ~[spring-data-mongodb-3.0.0.RELEASE.jar:3.0.0.RELEASE]
        at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:1992) ~[spring-data-mongodb-3.0.0.RELEASE.jar:3.0.0.RELEASE]
        at 
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 21, 2021
@christophstrobl
Copy link
Member

christophstrobl commented Dec 21, 2021

$sort will only consider fields that are actually part of the previous stage. In this case the $project stage does not include serial_number, which causes the error you see. Which version are you using? We relaxed the field exposure checks (see #3542).

@christophstrobl christophstrobl added status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged labels Dec 21, 2021
@1053531172
Copy link
Author

version is 3.0.0.RELEASE

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Dec 21, 2021
@christophstrobl christophstrobl added type: bug A general bug and removed status: feedback-provided Feedback has been provided labels Mar 10, 2023
@mp911de mp911de added this to the 4.0.4 (2022.0.4) milestone Mar 16, 2023
mp911de added a commit that referenced this issue Mar 16, 2023
Simplify field creation considering simplified projection expressions.

See #3917
Original pull request: #4328
mp911de pushed a commit that referenced this issue Mar 16, 2023
This commit fixes an issue where the context is not relaxed and errors on unknown fields if multiple stages of nesting contexts happen.

Closes #3917
Original pull request: #4328
mp911de added a commit that referenced this issue Mar 16, 2023
Simplify field creation considering simplified projection expressions.

See #3917
Original pull request: #4328
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants