Closed brenthaertlein closed 1 year ago
Nevermind, this was entirely my bad! I just happened to run into a case with my tests where $unwind
on an empty array produces no results and conflated that with a "embedded test behavior" issue.
@brenthaertlein no problem:) .. sometimes it is helpful to explain the problem to an other person just to solve it by yourself:)
When trying to compose an aggregation using Spring Data Mongo, it appears that including unwind / group stages in the aggregation pipeline causes a subsquent match with regex to... not work.
Given these Gradle dependencies:
Which results in using these versions:
Given this application configuration:
Given some class like:
Here is some pseudocode, but I can set up an example project next week if it is necessary.
In the above example, I would expect the
$match
stage to regex match againstname
which should be a top level property of each document in the pipeline after$group
.When I run my server and MongoDB locally, I get the behavior I expect. However, our integration tests for these types of queries failed after I converted from
mongoTemplate.find
tomongoTemplate.aggregate
. Initially I was very perplexed, and found that internally Spring Data Mongo seems to use the$regularExpression
operator which is part of the MongoDB Extended JSON (v2) specification.However, I found that simply by removing the
$group
stage and the preceding stages, the$match
stage does work in the integration tests. Unfortunately, we need to$unwind
,$sort
and$group
because we don't have access to$sortArray
which was introduced in MongoDB 5.2.The above code generates this pipeline, but returns no results even when it should:
When the
$group
and preceding stages are removed, we get this pipeline which works as expected:Let me know what you think and happy holidays!