-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Aggregations should be able to handle Sort.unsorted()
#4703
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
Comments
Thank you @bithazard for getting in touch. We'll look into this. |
I've been looking at the mongodb documentation and related tickets but there's no way to pass in something similar to |
I thought into this direction as well. Sorting by Of course the issue could be solved on the MongoDB side. They could simply allow Regarding the "silently ignore the stage": I think it would be more like skipping the stage because there is simply nothing to do. I understand that it's not great to add a special case for such a situation but I also looked a bit into the code and I think it would be possible to add a check to https://github.com/spring-projects/spring-data-mongodb/blob/main/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AggregationOperationRenderer.java#L56 to handle this. There is a loop that iterates over all Just a thought. I don't know the code well enough to say if that would cause issues elsewhere. |
Thanks for the feedback - the checks in |
I've added a suggestion to the "MongoDB Feedback Engine" that you can find under https://feedback.mongodb.com/forums/924280-database/suggestions/48473684-aggregations-should-allow-an-empty-sort-stage-inst. Feel free to give it a vote. If this gets implemented, the way that you currently generate the sort stage, will start working. I think that it also makes sense for MongoDB to handle an empty sort stage like this. A sort object with n keys will sort by these n fields, so an empty object should not sort by any field. Adding another special keyword (like I also found this section of the MongoDB sort aggregation documentation, where they recommend to add the //Some sort object you get from somewhere (could be Sort.unsorted())
Sort sort = ...;
mongoTemplate.aggregate(newAggregation(sort(sort.and(Sort.by("_id")))), "test", Document.class); |
Hi. When you create an aggregation pipeline and use a sort operation with
Sort.unsorted()
an exception is thrown (UncategorizedMongoDbException
). The reason for the exception is, that the sort stage is created but it is simply an empty object. You can reproduce the behavior with the following example:You can see that it works when you sort by an arbitrary field (
x
in the example above). It also works when usingfind
instead ofaggregate
. In this case thesort
operation never reaches the MongoDB(-driver) but is removed somewhere in between. This should also happen for an aggregation in my opinion.Of course a developer can make sure that an
unsorted
sort operation is never added to the pipeline (in a real world scenario the sort field will probably come from a URL parameter or something like that - so it's not as obvious as in the example) but that implies that the developer is aware that an exception will be thrown otherwise.The text was updated successfully, but these errors were encountered: