spring-projects / spring-data-mongodb

Provides support to increase developer productivity in Java when using MongoDB. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.
https://spring.io/projects/spring-data-mongodb/
Apache License 2.0
1.62k stars 1.09k forks source link

Query from method name has map entries in wrong order. #4577

Closed aschmalfeld closed 10 months ago

aschmalfeld commented 10 months ago

In my class which extends from MongoRepository I have a method

Optional<MyEntity> findByMyCategories(TreeMap<String, String> myCategories);

where myCategories are e.g.:

TreeMap<String, String> myCategories = new TreeMap<>(Map.of("test1", "123", "test2", "456"));

When inserted into the Mongodb the order of key value pairs is as expected from a TreeMap, but when I call the method findByMyCategories(myCategories) the mongoDB logs suggest the wrong order was used and therefore the object was not found.

"filter":{"myCategories":{"test2":"456","test1":"123"}}

This always returns the correct results for only one key value pair, for multiple pairs it is always wrong for some pairs and always correct for others as if it is following some pattern.

It worked correctly before the update of spring-data-mongodb from 4.1.6 to 4.2.0

A workaround is to annotate the method with @Query or @Aggregation.

christophstrobl commented 10 months ago

@aschmalfeld thanks for reporting - guess that will require some digging where things go off route.

christophstrobl commented 10 months ago

looks like this is a side effect of #4555. The current PR #4568 will fix the ordering aspect as well.

mp911de commented 10 months ago

That's fixed now.