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.59k stars 1.07k forks source link

Regex criteria is not mapped to mongo query #4691

Closed FakeNoxy closed 2 months ago

FakeNoxy commented 2 months ago

I am using Kotlin / Spring Data and I have a problem querying with regex using MongoTemplate.aggregate()

Creating the Criteria: override fun createFilterCriteria(value: String): Criteria = Criteria.where(AEHProduct::searchText.name).regex(value)

After passing everything to MongoTemplate.aggregate() (The criteria is added to the MatchOperation), and I can see in the server logs that it is parsed as well:

Executing aggregation: ... { "searchText" : { "$regularExpression" : { "pattern" : "hh4mvorvlhz", "options" : ""}}} ...

When I catch the query on the database side, it is empty, resulting in 0 results:

... { "searchText": {} } ...

Other filters (in, nin, eq, etc..) and sorting/skipping/grouping are working fine.

org.mongodb:bson version: 4.11.1 spring-boot-starter-data-mongodb version: from spring-boot-starter-parent 3.2.4