Skip to content

Support Mongo DB findOneAndUpdate#3777

Open
radovanradic wants to merge 35 commits into5.0.xfrom
radovanradic/issue-2023
Open

Support Mongo DB findOneAndUpdate#3777
radovanradic wants to merge 35 commits into5.0.xfrom
radovanradic/issue-2023

Conversation

@radovanradic
Copy link
Copy Markdown
Contributor

@radovanradic radovanradic commented Mar 25, 2026

Requested in #2023

@radovanradic radovanradic marked this pull request as ready for review March 25, 2026 12:36
Copilot AI review requested due to automatic review settings March 25, 2026 12:36
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds MongoDB support for single-document update operations that return the matched document (i.e., findOneAndUpdate) via a new @MongoUpdateReturningQuery annotation, along with runtime execution and corresponding docs/tests.

Changes:

  • Introduces @MongoUpdateReturningQuery and wires a new Mongo UPDATE_RETURNING execution path based on findOneAndUpdate.
  • Adds runtime query/option models (MongoFindOneAndUpdate, prepared/stored query APIs) and implements execution in DefaultMongoRepositoryOperations.execute.
  • Updates documentation and adds processor/runtime tests covering before/after return document, DTO/scalar projection, and sort selection.

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/main/docs/guide/mongo/mongoRepositories/mongoCustomQueries.adoc Documents @MongoUpdateReturningQuery, returnDocument behavior, and usage constraints.
data-mongodb/src/test/java/io/micronaut/data/document/mongodb/repositories/MongoPersonRepository.java Adds update-returning repository methods (entity/DTO/scalar, sort, options param).
data-mongodb/src/test/java/io/micronaut/data/document/mongodb/repositories/MongoBookRepository.java Adds book examples for update-returning and AFTER behavior.
data-mongodb/src/test/groovy/io/micronaut/data/document/mongodb/MongoDocumentRepositorySpec.groovy Adds integration tests for update-returning behavior and projections.
data-mongodb/src/main/java/io/micronaut/data/mongodb/operations/options/MongoOptionsUtils.java Adds FindOneAndUpdateOptions builder for update-returning operations.
data-mongodb/src/main/java/io/micronaut/data/mongodb/operations/MongoStoredQuery.java Adds stored-query API to retrieve update-returning execution data.
data-mongodb/src/main/java/io/micronaut/data/mongodb/operations/MongoPreparedQuery.java Adds prepared-query API to retrieve update-returning execution data.
data-mongodb/src/main/java/io/micronaut/data/mongodb/operations/MongoFindOneAndUpdate.java New value object representing findOneAndUpdate inputs (filter/update/options).
data-mongodb/src/main/java/io/micronaut/data/mongodb/operations/DefaultMongoStoredQuery.java Builds MongoFindOneAndUpdate from stored query + invocation context; merges options/projection/sort/collation.
data-mongodb/src/main/java/io/micronaut/data/mongodb/operations/DefaultMongoRepositoryOperations.java Implements execute() for UPDATE_RETURNING using collection.findOneAndUpdate.
data-mongodb/src/main/java/io/micronaut/data/mongodb/operations/DefaultMongoPreparedQuery.java Exposes update-returning data from stored query on prepared query.
data-mongodb/src/main/java/io/micronaut/data/mongodb/annotation/MongoUpdateReturningQuery.java New annotation defining update JSON, filter, projection, sort, collation, and returnDocument.
data-document-processor/src/test/groovy/io/micronaut/data/document/processor/BuildMongoQuerySpec.groovy Adds compile-time processor tests validating return-type constraints and metadata storage (e.g., sort).
data-document-processor/src/main/java/io/micronaut/data/document/processor/matchers/MongoRawQueryMethodMatcher.java Adds processor matching for @MongoUpdateReturningQuery and validates return-type constraints.
data-document-model/src/main/java/io/micronaut/data/document/mongo/MongoAnnotations.java Adds constant for MongoUpdateReturningQuery.
Comments suppressed due to low confidence (1)

data-document-processor/src/main/java/io/micronaut/data/document/processor/matchers/MongoRawQueryMethodMatcher.java:135

  • Update-returning repository methods can declare a FindOneAndUpdateOptions parameter (see tests), but the matcher never assigns a parameter role for it. Without MongoAnnotations.UPDATE_OPTIONS_ROLE being set for that parameter, DefaultMongoStoredQuery won’t be able to locate it (getParameterInRole(MongoRoles.UPDATE_OPTIONS_ROLE)), and the runtime FindOneAndUpdateOptions argument will be ignored.

Tangible fix: when operationType == UPDATE_RETURNING, detect a parameter of type com.mongodb.client.model.FindOneAndUpdateOptions and add it as MongoAnnotations.UPDATE_OPTIONS_ROLE (similar to how MongoExecutorQueryMethodMatcher handles UpdateOptions).

                ParameterElement[] parameters = matchContext.getParameters();
                ParameterElement entityParameter;
                ParameterElement entitiesParameter;
                if (parameters.length > 1) {
                    entityParameter = null;
                    entitiesParameter = null;
                } else {
                    entityParameter = Arrays.stream(parameters).filter(p -> TypeUtils.isEntity(p.getGenericType())).findFirst().orElse(null);
                    entitiesParameter = Arrays.stream(parameters).filter(p -> TypeUtils.isIterableOfEntity(p.getGenericType())).findFirst().orElse(null);
                }

…ver runtime options (#3778)

* Initial plan

* Rename misleading test to clarify annotation returnDocument precedence over options

Co-authored-by: radovanradic <10271067+radovanradic@users.noreply.github.com>
Agent-Logs-Url: https://github.com/micronaut-projects/micronaut-data/sessions/479c10cc-d017-4c2d-bdc5-7a89803859e9

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: radovanradic <10271067+radovanradic@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 15 out of 15 changed files in this pull request and generated 2 comments.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 25, 2026 14:32
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 15 out of 15 changed files in this pull request and generated 4 comments.

Copilot AI review requested due to automatic review settings March 25, 2026 15:43
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 16 out of 16 changed files in this pull request and generated 3 comments.

@radovanradic radovanradic requested a review from Copilot March 25, 2026 16:07
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 16 out of 16 changed files in this pull request and generated 3 comments.

Copilot AI review requested due to automatic review settings April 3, 2026 09:40
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 19 out of 19 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

data-mongodb/src/main/java/io/micronaut/data/mongodb/operations/MongoRoles.java:23

  • Typo in Javadoc: “contansts” should be “constants”.
/**
 * Mongo parameter roles contansts.
 *

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 3, 2026 09:49
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 19 out of 19 changed files in this pull request and generated 1 comment.

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 3, 2026

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • checkstyle.org
    • Triggering command: /opt/hostedtoolcache/CodeQL/2.24.3/x64/codeql/tools/linux64/java/bin/java /opt/hostedtoolcache/CodeQL/2.24.3/x64/codeql/tools/linux64/java/bin/java -jar /opt/hostedtoolcache/CodeQL/2.24.3/x64/codeql/xml/tools/xml-extractor.jar --fileList=/tmp/codeql-scratch-6a366402fabce74f/dbs/java/working/files-to-index8576617392778953732.list --sourceArchiveDir=/tmp/codeql-scratch-6a366402fabce74f/dbs/java/src --outputDir=/tmp/codeql-scratch-6a366402fabce74f/dbs/java/trap/java --global /home/REDACTED/.local/bin/git commit.gpgsign (dns block)
  • ge.micronaut.io
    • Triggering command: /usr/lib/jvm/temurin-21-jdk-amd64/bin/java /usr/lib/jvm/temurin-21-jdk-amd64/bin/java --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED --add-opens=java.base/java.nio.charset=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED --add-opens=java.xml/javax.xml.namespace=ALL-UNNAMED --add-opens=java.base/java.time=ALL-UNNAMED -XX:MaxMetaspaceSize=2000m -Dfile.encoding=UTF-8 -Duser.country -Duser.language=en -Duser.variant (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Apr 3, 2026

Quality Gate Failed Quality Gate failed

Failed conditions
4 New Critical Issues (required ≤ 0)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants